asp.net 中的 Repeater 嵌套数据列表中的 LinkBut​​ton 单击事件

LinkButton Click Event in Repeater Nested Datalist in asp.net

我在项目中有这样的设计。如何在 asp.net 中的中继器嵌套数据列表中访问按钮单击事件?

<asp:DataList ID="dlPosts" runat="server" Width="100%" RepeatLayout="Flow" 
RepeatColumns="1" OnItemCommand="dlPosts_ItemCommand" 
OnItemDataBound="dlPosts_ItemDataBound">
 <ItemTemplate>
   <asp:Repeater ID="repImgs" runat="server">
    <ItemTemplate>
     <img src="<%#Eval("Picture") %>"  style="height: 35px; width: 35px" alt="" align="middle" valign="top" />
     <asp:LinkButton ID="lbYorum" runat="server" class="w3-btn w3-green w3-hover-orange" CommandName="MyUpdate" CommandArgument='<%#Bind("YazarID") %>'> Send</asp:LinkButton>
    </ItemTemplate>
   </asp:Repeater>
 </ItemTemplate>
</asp:DataList>

您可以像常规按钮控件一样将 OnCommand 事件附加到 LinkBut​​ton。

<asp:LinkButton ID="lbYorum" runat="server" 
    class="w3-btn w3-green w3-hover-orange" 
    CommandName="MyUpdate" 
    CommandArgument='<%#Bind("YazarID") %>' 
    OnCommand="lbYorum_Command"> Send</asp:LinkButton>

代码隐藏

然后从 e.CommandArgument 检索 YazarID

protected void lbYorum_Command(object sender, CommandEventArgs e)
{
    string commandName = e.CommandName;
    string yazarID = e.CommandArgument.ToString();
}

* 更新 *

if I add a new TextBox in repeater, how can I get the value of it?

您可以使用 Parent.FindControl 来查找同级控件。

...

<ItemTemplate>
    <img src="<%#Eval("Picture") %>" style="height: 35px; width: 35px" alt="" align="middle" valign="top" />
    <asp:LinkButton ID="lbYorum" runat="server" 
        class="w3-btn w3-green w3-hover-orange" 
        CommandName="MyUpdate" 
        CommandArgument='<%#Bind("YazarID") %>' 
        OnCommand="lbYorum_Command"> Send</asp:LinkButton>
    <asp:TextBox ID="txtYorum" runat="server" Height="50" Width="500" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
...

protected void lbYorum_Command(object sender, CommandEventArgs e)
{
    string commandName = e.CommandName;
    string yazarID = e.CommandArgument.ToString();

    var control = sender as Control;
    var txtYorum = control.Parent.FindControl("txtYorum") as TextBox;
}