Assign selectedindex of datalist with datakey value

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
Who is Participating?
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.

rygar2006Author Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.