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: MouseOver Coloring for a DataGrid
Views: 16726
Article: Retain Scrollback Position After Postback in DataGrid
Views: 6464
Article: Hierarchical Data Binding in ASP.NET
Views: 9422
Article: Common DataGrid Mistakes
Views: 18940
Article: Transferring the Datagrid Data Between Web Forms
Views: 5552
Article: Summarizing Data with ROLLUP
Views: 7893
Article: Displaying Records in a DataGrid in Random Order
Views: 6477
Article: Adding a New Record to the DataGrid in the Footer
Views: 12733
Article: Creating Custom Columns for the ASP.NET Datagrid
Views: 14696
Article: Deciding When to Use the DataGrid, DataList or Repeater
Views: 7298


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...]