Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1177
  • Last Modified:

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
1 Solution
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
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.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now