Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 如何从中继器的第一行获取数据属性值列表?_Asp.net_Vb.net_Repeater_Custom Data Attribute - Fatal编程技术网

Asp.net 如何从中继器的第一行获取数据属性值列表?

Asp.net 如何从中继器的第一行获取数据属性值列表?,asp.net,vb.net,repeater,custom-data-attribute,Asp.net,Vb.net,Repeater,Custom Data Attribute,如何在.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

如何在.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
  • 一,
  • 二,
  • 三,
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>

我可以很容易地循环第一行中的每个面板,但无法确定如何读取面板的数据属性。

请记住,在处理程序中创建的任何变量都将在每次调用时重新创建,除非变量的范围大于处理程序

我使用了
ctrl作为面板
,让它很开心

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

您是否尝试使用
静态
变量作为标志来指示它是第一行?或者您正在寻找
someControl.Attributes(“数据id”)
?@AndrewMorton点击第一行就可以了。问题是读取面板的数据属性。它抱怨类型控件没有属性。我也尝试了
htmlGenericControl
WebControl
,但找不到数据属性字段的存储位置。我所要做的就是将其更改为面板。哇!谢谢你,先生,你是一个传奇。@DreamTeK,不客气:)起初我用了
Dim pnl=DirectCast(ctrl,Panel)
pnl.Attributes.Item(“数据id”)
,然后我把它删减了,认为
是控件
-这不应该是面板中的
,因为这就是选择的目的吗?