Reputation: 8225
I have this repeater:
<asp:Repeater ID="rptImages" runat="server">
<HeaderTemplate>
<table>
<tr>
<th>Image</th>
<th>Caption</th>
<th> </th>
<th>Update File</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Image Width="108px" Height="67px" runat="server" ID="imgDb" ImageUrl='<%# Eval("imageUrl") %>' />
</td>
<td>
<asp:TextBox runat="server" style="margin-left: 5px; margin-right: 5px;" ID="txtCaption" Text='<%# Eval("caption") %>'></asp:TextBox>
</td>
<td>
<td><asp:FileUpload ID="fu" runat="server" /></td>
<td><asp:Button ID="btn" runat="server" Text="Update" CssClass="btn btn-info" CommandArgument='<%# Eval("id") %>' OnClick="btn_OnClick" /></td>
<td><asp:Button runat="server" ID="btnDelete" CssClass="btn btn-danger" CommandArgument='<%# Eval("id") %>' OnClick="btnDelete_OnClick" Text="Delete" /></td>
<td><asp:HiddenField ID="lblC" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "id") %>'></asp:HiddenField></td>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I want to know how can I get the textbox specifically from the row where the update button is clicked. What logic should I use to get the value I need?
Upvotes: 0
Views: 5433
Reputation: 56688
You could probably do that with handling Click
event on the button and then getting its parent control. But I would recommend handling command of the repeater instead:
<asp:Repeater ID="rptImages" runat="server"
OnItemCommand="rptImages_ItemCommand">
Remove the handler from the button, but keep the command arg. You may also want to set command name if there are other controls sending commands from within the repeater:
<asp:Button runat="server" ID="btnDelete" CssClass="btn btn-danger"
CommandArgument='<%# Eval("id") %>'
Text="Delete" />
And in the code behind this becomes trivial:
protected void rptImages_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
TextBox t = e.Item.FindControl("txtCaption") as TextBox;
}
Upvotes: 4