I've searched the internet and didn't find a working answer for my question.
The question is when I use a text-box and a button in an ASP:Repeater for my C# ASPX Website to update a column of SQL Server table, I can't access the textbox and the button in the code-behind (ASPX.CS) page. However I've find a way to "show" those controllers (the text-box and the button) in the ASP:Repeater in the link below, but I can't actually use those to update the database of the website.
Can't find control within asp.net repeater?
I'd be glad if anyone know how to solve this problem, reply this.
Here is a sample code of demonstrating the problem:
ASPX Page
<asp:Repeater runat="server" ID="Commentha" >
<ItemTemplate>
<div id="comment list" style="direction: rtl; font-family: '2 Nazanin','Adobe Arabic'; font-size: large;">
<div id="Replys">
<%--ASP LABEL FOR SHOWING ALL REPLYS FOR THIS COMMENT--%>
</div>
<div id="ReplyTo">
<div id="RT_Text">Reply</div>
<div id="ReplyToThis">
<asp:TextBox runat="server" ID="Reply" MaxLength="119"></asp:TextBox>
<asp:Button runat="server" ID="Sendit" Text="Send" />
</div>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
The "Reply" textbox and "Sendit" buttons are the controllers I was talking about.
And the SQL Table has this column:
ReplyTo nvarchar(120)
I am actually trying to make a "Reply to a comment" function, something similar to Facebook's "reply" button for the commenting section.
You need to look into the Events associated with the repeater. Especially on ItemCommand. Below is a snippet that will allow you to grab the textbox.
Added CommandName to the button allows you to assign requests made from it to specific actions using the ItemCommand event handler.
<asp:Button ID="Sendit" runat="server" Width="80px" Text="Send" CommandName="Send"/>
Add this event handler. So when that button is clicked it will apply the logic within the if statement. You can grab controls within that row using FindControl. Then access them by its object variable. In this case txt.
protected void Commentha_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Send")
{
TextBox txt = e.Item.FindControl("Reply") as TextBox ;
string replyText = txt.Text;
// Do SQL
}
}