Programmatically Selecting the SelectedIndex

This live demo illustrates how to programmatically set the SelectedIndex based on the data to be bound to the data Web control. Specifically, this example looks at auto-selecting the article with the most visits... (Note that only 10 rows are retrieved, and that they are ordered randomly using NEWID(). For more information on returning a random subset of rows with NEWID() be sure to read Returning Data in Random Order with NEWID(). Be sure to refresh to see the effect...)


Article: Creating a Fully Editable DataGrid
Views: 23757
Article: Displaying Records in a DataGrid in Random Order
Views: 5927
Article: Retain Scrollback Position After Postback in DataGrid
Views: 5934
Article: Binding a DataGrid to an ADO Recordset
Views: 4683
Article: An Extensive Examination of the DataGrid Web Control: Part 1
Views: 21722
Article: Deciding When to Use the DataGrid, DataList or Repeater
Views: 6766
Article: Hierarchical Data Binding in ASP.NET
Views: 8900
Article: Creating a Custom DataGridColumn Class
Views: 4373
Article: Summarizing Data with ROLLUP
Views: 7327
Article: Top Questions About the DataGrid Web Control
Views: 15826


Source Code

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page Language="VB" %>
<script runat="server">

    Sub Page_Load(sender as Object, e as EventArgs)
           If Not Page.IsPostBack then
             dlArticles.DataSource = GetArticles()
             dlArticles.DataBind()
           End If
         End Sub
    
    
         Function GetArticles() As DataTable
             Dim connectionString As String = "connectionString"
             Dim sqlConnection As SqlConnection = New SqlConnection(connectionString)
    
             Dim queryString As String = "SELECT TOP 10 ArticleID, [Articles].[Title], [Articles].[URL], [Articles].[dateAuthored], [Articles"& _
    "].[Comments], [Articles].[ClickThroughs] FROM [Articles] ORDER BY NEWID()"
             Dim sqlCommand As SqlCommand = New SqlCommand(queryString, sqlConnection)
             Dim sqlAdapter as SqlDataAdapter = New SqlDataAdapter(sqlCommand)
             
             Dim dt as New DataTable()
             sqlAdapter.Fill(dt)
             
             'Find the row with the most views
             Dim highViewCount = 0, highViewIndex = -1, i as Integer
             For i = 0 to dt.Rows.Count - 1
               If dt.Rows(i)("ClickThroughs") > highViewCount then
                 highViewCount = dt.Rows(i)("ClickThroughs")
                 highViewIndex = i
               End If
             Next i
             
             'Set the selectedindex
             dlArticles.SelectedIndex = highViewIndex
    
             Return dt
         End Function
</script>


<asp:DataList id="dlArticles" runat="server" AutoGenerateColumns="False" Font-Size="10pt" Font-Names="Verdana">
    <HeaderStyle font-size="13pt" font-bold="True" horizontalalign="Center" forecolor="White" backcolor="#006699"></HeaderStyle>
    <AlternatingItemStyle backcolor="#EEEEEE"></AlternatingItemStyle>
    <ItemTemplate>
		<b>Article:</b> <%# DataBinder.Eval(Container.DataItem, "Title") %><br>
		<b>Views:</b> <%# DataBinder.Eval(Container.DataItem, "ClickThroughs") %>
	</ItemTemplate>
	<SelectedItemTemplate>
		<font size="+1">
			<b>Article:</b> <%# DataBinder.Eval(Container.DataItem, "Title") %><br>
			<span style="background-color:yellow;"><b>Views:</b> <%# DataBinder.Eval(Container.DataItem, "ClickThroughs") %></span>
		</font>
	</SelectedItemTemplate>
</asp:DataList> 
    

[Return to the FAQ...]