Access textbox in itemtemplate

Total Post:128

Points:898
 1055  View(s)
Ratings:
Rate this:

I have 2 textboxes and a button in a row, and a Repeater dynamically generates some rows at Page_Load function.

<asp:Repeater id="Repeater1" runat="server">
    <ItemTemplate>
        <tr>
            <td><asp:Label CssClass="form-control" disabled="true" runat="server"><%# DataBinder.Eval(Container.DataItem, "sid") %></asp:Label></td>
            <td><asp:TextBox CssClass="form-control" runat="server" ID="quiz1"></asp:TextBox></td>
            <td><asp:TextBox CssClass="form-control" runat="server" ID="quiz2"></asp:TextBox></td>
            <td><asp:Button ID="add" CommandName="add" runat="server" OnClick="addQuiz" Text="Add" CssClass="btn btn-success btn-sm form-control"/></td>
        </tr>
     </ItemTemplate>
</asp:Repeater>

How do I access the textboxes in the Repeater upon clicking the button in the row?

  1. Post:412

    Points:2888
    Re: Access textbox in itemtemplate

    Hi Ashish, 

    try this:

    protected void addQuiz(Object sender, EventArgs e)
    {
        var btn = (Button) sender;
        var item =(RepeaterItem) btn.NamingContainer;
        var quiz1 =(TextBox) item.FindControl("quiz1");
        var quiz2 =(TextBox) item.FindControl("quiz2");
    }

      Modified On Apr-04-2018 05:59:33 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!