2

Can you please tell me what's wrong with the following code?

        Panel div = new Panel();
        Button btn1 = new Button { Text = "Delete", CommandArgument = "argument", ID = "remove" };
        Button btn2 = new Button { Text = "Insert", CommandArgument = "argument2", ID = "insert" };

        btn1.Click += new EventHandler(btn_click);
        btn2.Click += new EventHandler(btn_click);

        div.Controls.Add(btn1);
        div.Controls.Add(btn2);

        ph_plan.Controls.Add(div); // where ph_plan is a placeholder in the user control

protected void btn_click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    if(btn.ID == "remove")
        // do this
    else 
        // do that
}

The code above occurs right after a click on a button in the user form. It is supposed to create 2 new buttons with events assigned. Indeed, it creates the buttons but when I click them nothing happens. I guess the events cannot be registered. What am I doing wrong here?

4
  • Can we see the btn_click code for good measure? Commented May 9, 2011 at 15:53
  • Can you show us the code for btn_click? Commented May 9, 2011 at 15:54
  • of course. I'm editing right now Commented May 9, 2011 at 15:55
  • done, but please bear in mind that the problem is that the event is not fired. Commented May 9, 2011 at 15:58

2 Answers 2

6

The reason this is happening is because Page is a stateless class and once it renders everything, it is destroyed. Therefore, once you have a postback, this information is lost and your Page class has no knowledge of the button's events since the dynamic buttons were not part of the aspx file.

You need to maintain a collection of the dynamic controls that you've created, possibly in a session, so that they can be recreated after a postback. There's an example of it here.

Sign up to request clarification or add additional context in comments.

3 Comments

E.g. the buttons need to be recreated on post-back otherwise they don't exists and hence, neither does the event wiring.
Exactly. Although ASP.NET development is similar to WinForms in terms of creating and manipulating objects, the thought process is very different and it's easy to forget the stateless nature of the web.
ok, thanks done :) I've created a cache after the buttons are created and in the page_init event if the cache in question is found it re-creates the buttons. Now, it's working! Thanks again!
0

How to create multiple control in asp.net with event:

string[] arg = new string[10];

protected void Page_Load(object sender, EventArgs e)
{

    for (int i = 0; i < 10; i++)
    {
        LinkButton bb = new LinkButton();
        arg[i]= bb.ID = "bb" + i.ToString();
        bb.Text = "like"+"<br/>";
        Panel1.Controls.Add(bb);
        bb.Click += new EventHandler(bb_Click);
    }
}

void bb_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)sender;
    for (int j = 0; j < 10; j++)
    {
        if (btn.ID == arg[j])
        {
            btn.Text = "";
            btn.Text = "unlike";
            Response.Write(arg[j]);
        }
    }
}

This code will print every button id that have been created at runtime in page load event.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.