开发者

Getting value of anchor tag from repeater?

开发者 https://www.devze.com 2023-03-25 01:52 出处:网络
I have a regular html anchor link that is bound to an Id column.I want to loop through the repeater and get the value of the Id column, but can\'t figure out how.I have some code below my repeater mar

I have a regular html anchor link that is bound to an Id column. I want to loop through the repeater and get the value of the Id column, but can't figure out how. I have some code below my repeater markup. I can't figure out how to do it with just a client side anchor tag.

<asp:Repeater ID="repSearchResults" runat="server">
    &开发者_如何学运维lt;ItemTemplate>
        <tr>
           <td><a href='<%#Eval("Id")%>'><%#Eval("Id")</a></td>
        </tr>
</asp:Repeater>

Protected Sub btnGetIds_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    For Each item As RepeaterItem In repSearchResults.Items
        If (item.ItemType = ListItemType.Item) Then

           'Get Id here

        End If



    Next
End Sub


You could add a hidden field inside each template:

<ItemTemplate>
    <asp:HiddenField ID="hid" runat="server" Value='<%#Eval("Id")%>' />
    ...
</ItemTemplate>

and then inside the loop:

If item.ItemType = ListItemType.Item Then
    Dim ctrl As HiddenField = TryCast(item.FindControl("hid"), HiddenField)
        If ctrl IsNot Nothing Then
            Dim id As String = ctrl.Value
            ' do something with the id
    End If
End If


You would need to make the anchor runat=server, name it, and then access it with the FindControl method.

0

精彩评论

暂无评论...
验证码 换一张
取 消