Weird behavior with ASP.NET dynamic event binding

J

Jonathan Yong

I observe a very weird behavior when dynamically create web control
and bind events to it.

Create a C# ASP.NET application,
Put a PlaceHolder and Textbox onto the Web form,
and try with the 4 code scenerio below.

---------------------------------------------------------------------------

Scenerio 1

private void Page_Load(object sender, System.EventArgs e)
{
Button but = new Button();

// This is to simulate that IsPostBack will return false
// when the page is loaded for the first time.
bool a = false;

if(false == a)
{
but.Text = "Button 1";
but.Click += new System.EventHandler(this.Button1_Click);
}

this.PlaceHolder1.Controls.Add(but);
}


private void Button1_Click(object sender, System.EventArgs e)
{
TextBox1.Text = "button 1 Click";
}

When you click the button, the event get fired as expected.

---------------------------------------------------------------------------

Scenerio 2

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Button but = new Button();

if(false == this.IsPostBack)
{
// This line will get executed.
but.Text = "Button 1";

// This line does not seem to have any effect.
but.Click += new System.EventHandler(this.Button1_Click);
}

this.PlaceHolder1.Controls.Add(but);
}

When you click the button, the event is not fired. Looks like
the event handler is not binded to the button control.

---------------------------------------------------------------------------

Scenerio 3

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Button but = new Button();

bool b = IsPostBack;

if(false == b)
{
// This line will get executed.
but.Text = "Button 1";

// This line does not seem to have any effect.
but.Click += new System.EventHandler(this.Button1_Click);
}

this.PlaceHolder1.Controls.Add(but);
}


When you click the button, the event is not fired. Looks like the
event handler is not binded to the button control. Same as scnerio 2.

---------------------------------------------------------------------------

Scenerio 4

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Button but = new Button();

if(false == this.IsPostBack)
{
// This line will get executed.
but.Text = "Button 1";
}

// This line will have effect.
but.Click += new System.EventHandler(this.Button1_Click);

this.PlaceHolder1.Controls.Add(but);
}

When you click the button, the event get fired as expected. Same as
scnerio 1.

---------------------------------------------------------------------------

My point is when the expression in the IF statement have something to do
with IsPostBack property, the event binding code does not seem to have
any effect if executed inside the IF block.

Is there something that I don't know about event binding or is this a
ASP.NET bug ?
Same behavior is observed both in C# and VB.NET web app.
 
A

Alvin Bruney

This behavior is by design. The long and short of it is everytime you
dynamically create a button you need to manually wire up the events like in
scenario 3. This is because the button needs to be recreated everytime
whether or not the page is new or recreated. It does not automatically
remember to put itself on the form like an ordinary button will because you
haven't hooked it to view state which tells it to remember itself. If it
doesn't remember to put itself on the form, it won't remember to hook itself
to the click event, because it isn't there in the first place.

It's not a bug, it's due to the stateless nature of web applications. If you
are coming from a windows environment where stateful is the norm, I'm sure
this behavior seems a bit wierd and out of place. Its a perspective thing.

regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top