Link to home
Start Free TrialLog in
Avatar of rygar2006
rygar2006

asked on

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">
<ItemTemplate>
      <LI>
            <A href='index.aspx?pageID=<%# container.dataitem("pageID")%>'>
            <%# container.dataItem("pagetitle")%>
            </A>            
      </LI>
</ItemTemplate>
</asp:datalist>

'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
            Next
end if
Avatar of rygar2006
rygar2006

ASKER

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
        Next
    End Sub
ASKER CERTIFIED SOLUTION
Avatar of EE_AutoDeleter
EE_AutoDeleter

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial