I have such kind of a design in a project. How can I reach button click event in repeater nested datalist in 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>
You can just attach OnCommand event to LinkButton like regular button control.
<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>
Then retrieve YazarID
from e.CommandArgument
.
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?
You can use Parent.FindControl to find sibling controls.
...
<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;
}