我在项目中有这样的设计。如何在asp.net中的转发器嵌套数据列表中触及按钮单击事件?
.
答案 0 :(得分:1)
您可以像常规按钮控件一样将 OnCommand 事件附加到LinkButton。
<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>
然后从YazarID
检索e.CommandArgument
。
protected void lbYorum_Command(object sender, CommandEventArgs e)
{
string commandName = e.CommandName;
string yazarID = e.CommandArgument.ToString();
}
如果我在转发器中添加一个新的TextBox,我怎样才能得到它的值?
您可以使用 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;
}