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: Including Subheadings in a Datagrid
Views: 7203
Article: Creating a Fully Editable DataGrid
Views: 13425
Article: An Extensive Examination of the DataGrid Web Control: Part 1
Views: 21152
Article: Summarizing Data with ROLLUP
Views: 7065
Article: Top Questions About the DataGrid Web Control
Views: 15318
Article: DataGrid Made Compliant with Section 508 of the Web Accessibility Initiative Guidelines
Views: 5936
Article: Highlighting Search Keywords in a DataGrid Web Control
Views: 4594
Article: Creating Custom Columns for the ASP.NET Datagrid
Views: 13545
Article: Adding a DropDownList Web Control to the DataGrid's Editing Interface
Views: 6526
Article: Adding a New Record to the DataGrid in the Footer
Views: 11653


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