0

So, I have a repeater that prints out category names from database, each of those category names have under it TextBox and button, so that I could edit its name If I would like to, but... yeah, there is always one but. I simple can't reach the TextBox which is repeated, it tells me that TextBox_Update does not exist in current context.

    protected void Button1_Click(object sender, EventArgs e)
{

    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
    {
        SqlCommand cmd = new SqlCommand();

        cmd.Connection = conn;
        cmd.CommandText = @"UPDATE [categories] SET [category_name] = @category_name WHERE [category_id] = @category_id";
        cmd.Parameters.AddWithValue("@category_name", TextBox1.Text);
        cmd.Parameters.AddWithValue("@category_id", Request["id"]);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    Response.Redirect("default.aspx");

}

This is the way I tried to update it but... tells me that textbox does not exist at all.

Here is how it looks like in html

<asp:Repeater ID="Repeater_Categories" runat="server" >
    <HeaderTemplate>

       <div class="insertPost">
            <h1>Categories</h1>
           <hr />
    </HeaderTemplate>
    <ItemTemplate>
           <div class="category">
                <h3><%# Eval("category_name") %></h3>
                <div class="categoryEdit">
                    <asp:TextBox ID="TextBox1" runat="server"     ValidationGroup="3"></asp:TextBox>

                    <br />
                    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" ValidationGroup="3" />
                    <a href="?action=delete&id=<%# Eval("category_id") %>">Del</a>
                    <br />
                </div>
           </div>
    </ItemTemplate>
    <FooterTemplate>
</div>
    </FooterTemplate>
</asp:Repeater>

Probably doing something terribly wrong, I'm desperated for help ;<

@Edit

Fixed TextBox names..

kirtan
  • 289
  • 4
  • 13
Harugawa
  • 539
  • 5
  • 19
  • 1
    You googled for 2 hours? Really? Tons of [answers](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=find%20textbox%20in%20repeater%20c%23) out there. – Rick S Mar 03 '16 at 18:05
  • Possible duplicate of [Accessing Textboxes in Repeater Control](http://stackoverflow.com/questions/2484806/accessing-textboxes-in-repeater-control) – JosephStyons Mar 03 '16 at 18:24

1 Answers1

0

You should search for the TextBox to get a reference, and then use that reference to update its properties.

TextBox txt1 = Repeater_Categories.FindControls("TextBox1") as TextBox;
Christian Stewart
  • 15,217
  • 20
  • 82
  • 139
Pragnesh
  • 77
  • 2