locked
Adding evenhandlers to dynamically created buttons RRS feed

  • Question

  • User1942060492 posted

    Hey guys!

    I´ve been trying to get this thing to work, but no luck.

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["webshopConnectionString"].ConnectionString);
            
        protected void Page_Load(object sender, EventArgs e)
        {
                Button button;
                conn.Open();
                SqlCommand command = conn.CreateCommand();
                command.CommandText = "SELECT * FROM category";
    
                SqlDataReader thisReader = command.ExecuteReader();
    
                while (thisReader.Read())
                {
                    TableRow tr = new TableRow();
                    Table1.Rows.Add(tr);
                    TableCell tid = new TableCell();
                    tr.Cells.Add(tid);
                    tid.Text = thisReader["categoryid"].ToString();
                    TableCell tname = new TableCell();
                    tr.Cells.Add(tname);
                    TextBox name = new TextBox();
                    name.ID = "TextBox" + thisReader["categoryid"].ToString();
                    name.Text = thisReader["category_name"].ToString();
                    tname.Controls.Add(name);
                    button = new Button();
                    button.Click += new EventHandler(SaveBtn_Click);
                    button.Text = "Button" + thisReader["categoryid"].ToString();
                    button.ID = thisReader["categoryid"].ToString();
    
                    TableCell tedit = new TableCell();
                    tr.Cells.Add(tedit);
                    tedit.Controls.Add(button);
    
                }
            
            
        }
    
        protected void SaveBtn_Click(Object sender, EventArgs e)
        {
    
            Button clickedbutton = (Button)sender;
            Label1.Text = "clicked";
            
        }

    As you can see i am selecting everything from a SQL table and trying to add an edit button on every row. I want every button to have it´s own eventhandler that point to the same event, but as it is now i doesnt work at all.

    Any ideas?

    Saturday, October 13, 2012 6:26 PM

Answers

  • User-650628323 posted

    Hi, 

    I have tried to reproduce your issue and it seems like that it works well.  Please see the demo below and test it locally.

    In the .aspx

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Table ID="Table1" runat="server">
            </asp:Table>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        </form>
    </body>
    </html>

    In the .cs

     protected void Page_Load(object sender, EventArgs e)
        {           
            Button button;
            int i = 3;       
            while (i!=1)
            {
                TableRow tr = new TableRow();
                Table1.Rows.Add(tr);
                TableCell tid = new TableCell();
                tr.Cells.Add(tid);
                tid.Text = "tidText"+i;
                TableCell tname = new TableCell();
                tr.Cells.Add(tname);
                TextBox name = new TextBox();
                name.ID = "nameID"+i;
                name.Text = "nameText"+i;
                tname.Controls.Add(name);
                button = new Button();
                button.Click += new EventHandler(SaveBtn_Click); 
    
                button.Text = "buttonText"+i;
                button.ID = "buttonID"+i;
                TableCell tedit = new TableCell();
                tr.Cells.Add(tedit);
                tedit.Controls.Add(button);
                i--;              
            }
            form1.Controls.Add(Table1);
         }
    
         protected void SaveBtn_Click(Object sender, EventArgs e)
        {
            Button clickedbutton = (Button)sender;
            Label1.Text = "clicked";
        }

    If it works, please try to set a break point at the code below at first. Then use IE Developer Tools to debug step by step and see whether thisReader.Read() isn't null.

    while (thisReader.Read())

    And put a break point at the code below and see whether the event is clicked.

    Button clickedbutton = (Button)sender;

    Best wishes,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 18, 2012 5:51 AM

All replies

  • User451486883 posted

    Hi Desell,

      Your code looks pretty fine the only thing I did not understand is whether you are able to clicke the button and the page is posting back once you click?

    if so the thing here is you need to recall the same control creation and eventhandler assignment again on postback to fire the event. other wise it wont fire the event.

    Thanks

    Sunday, October 14, 2012 1:51 AM
  • User-650628323 posted

    Hi, 

    I have tried to reproduce your issue and it seems like that it works well.  Please see the demo below and test it locally.

    In the .aspx

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Table ID="Table1" runat="server">
            </asp:Table>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </div>
        </form>
    </body>
    </html>

    In the .cs

     protected void Page_Load(object sender, EventArgs e)
        {           
            Button button;
            int i = 3;       
            while (i!=1)
            {
                TableRow tr = new TableRow();
                Table1.Rows.Add(tr);
                TableCell tid = new TableCell();
                tr.Cells.Add(tid);
                tid.Text = "tidText"+i;
                TableCell tname = new TableCell();
                tr.Cells.Add(tname);
                TextBox name = new TextBox();
                name.ID = "nameID"+i;
                name.Text = "nameText"+i;
                tname.Controls.Add(name);
                button = new Button();
                button.Click += new EventHandler(SaveBtn_Click); 
    
                button.Text = "buttonText"+i;
                button.ID = "buttonID"+i;
                TableCell tedit = new TableCell();
                tr.Cells.Add(tedit);
                tedit.Controls.Add(button);
                i--;              
            }
            form1.Controls.Add(Table1);
         }
    
         protected void SaveBtn_Click(Object sender, EventArgs e)
        {
            Button clickedbutton = (Button)sender;
            Label1.Text = "clicked";
        }

    If it works, please try to set a break point at the code below at first. Then use IE Developer Tools to debug step by step and see whether thisReader.Read() isn't null.

    while (thisReader.Read())

    And put a break point at the code below and see whether the event is clicked.

    Button clickedbutton = (Button)sender;

    Best wishes,

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 18, 2012 5:51 AM