Assign selectedindex of datalist with datakey value

Posted on 2006-05-09
Last Modified: 2012-06-27
I have a nagivation bar created with a datalist. Each item of the datalist has a link with a querystring value with the name pageID which is the same value as the datakey for that item.

I'm trying to select an item in a datalist by comparing the datalist items datakey value with the pageID value from the querystring.

<asp:datalist id="dlstNav" runat="server" DataKeyField="pageID">
            <A href='index.aspx?pageID=<%# container.dataitem("pageID")%>'>
            <%# container.dataItem("pagetitle")%>

'Receives that querystring value
Dim pageID As Integer
        pageID = Request.QueryString("catID")

If Not catID = Nothing Then
            Dim item As DataListItem
            For Each item In dlstNav.Items
                If item.(get value of datakey) = pageID Then
                          dlstNav.selectedindex = item.itemindex
                end if
end if
Question by:rygar2006

    Author Comment

    I figure it out. - the For Each sub was not firing in the page_load but it works in the datalist_load. I got the value of the datakey by binding it onto a label then use the findcontrol method to retreive it for each item in the datalist.

     Private Sub dlstNav_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles dlstNav.Load
            Dim catID As Integer
            catID = Request.QueryString("catID")

            Dim lblcatID As Label

            For Each li As DataListItem In dlstNav.Items
                lblcatID = li.FindControl("lblcatID")
                If lblcatID.Text = catID Then
                    dlstNav.SelectedIndex = li.ItemIndex
                End If
        End Sub

    Accepted Solution

    Because you have presented a solution to your own problem which may be helpful to future searches, this question is now PAQed and your points have been refunded.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now