如何从转发器的第一行获取数据属性值列表?
How to get a list of data-attribute values from the first row of a repeater?
如何在 .NET 中读取控件的数据属性?
期望的结果
- 1
- 2
- 3
ASPX
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:Panel ID="pnlItem" runat="server">
<asp:Panel ID="pnlA" CssClass="pnl-class" data-id="1" runat="server"></asp:Panel>
<asp:Panel ID="pnlB" CssClass="pnl-class" data-id="2" runat="server"></asp:Panel>
<asp:Panel ID="pnlC" CssClass="pnl-class" data-id="3" runat="server"></asp:Panel>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
VB.NET
Private Sub rpt_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rpt.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
If e.Item.ItemIndex = 0 Then 'First item only
For Each control As Control In e.Item.FindControl("pnlItem").Controls.OfType(Of Panel)
'How do you read the attributes from the panel here?
Next
End If
End If
End Sub
我可以轻松遍历第一行中的每个面板,但不知道如何读取面板的数据属性。
请记住,您在处理程序中创建的任何变量都将在每次调用它时重新创建,除非变量的范围大于处理程序。
我用ctrl As Panel
让它开心
Private Sub rpt_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rpt.ItemDataBound
Static sb As New StringBuilder()
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
If e.Item.ItemIndex = 0 Then 'First item only
For Each ctrl As Panel In e.Item.FindControl("pnlItem").Controls.OfType(Of Panel)
Dim d = ctrl.Attributes("data-id")
sb.Append(d)
Next
End If
End If
' Do something with sb N.B. it is called for every rpt.ItemDataBound event.
' "msg" is an asp:Literal I put on the page for testing.
msg.Text = sb.ToString()
End Sub
如何在 .NET 中读取控件的数据属性?
期望的结果
- 1
- 2
- 3
ASPX
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:Panel ID="pnlItem" runat="server">
<asp:Panel ID="pnlA" CssClass="pnl-class" data-id="1" runat="server"></asp:Panel>
<asp:Panel ID="pnlB" CssClass="pnl-class" data-id="2" runat="server"></asp:Panel>
<asp:Panel ID="pnlC" CssClass="pnl-class" data-id="3" runat="server"></asp:Panel>
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
VB.NET
Private Sub rpt_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rpt.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
If e.Item.ItemIndex = 0 Then 'First item only
For Each control As Control In e.Item.FindControl("pnlItem").Controls.OfType(Of Panel)
'How do you read the attributes from the panel here?
Next
End If
End If
End Sub
我可以轻松遍历第一行中的每个面板,但不知道如何读取面板的数据属性。
请记住,您在处理程序中创建的任何变量都将在每次调用它时重新创建,除非变量的范围大于处理程序。
我用ctrl As Panel
让它开心
Private Sub rpt_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles rpt.ItemDataBound
Static sb As New StringBuilder()
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
If e.Item.ItemIndex = 0 Then 'First item only
For Each ctrl As Panel In e.Item.FindControl("pnlItem").Controls.OfType(Of Panel)
Dim d = ctrl.Attributes("data-id")
sb.Append(d)
Next
End If
End If
' Do something with sb N.B. it is called for every rpt.ItemDataBound event.
' "msg" is an asp:Literal I put on the page for testing.
msg.Text = sb.ToString()
End Sub