469,902 Members | 1,939 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

Weird behavior with ASP.NET dynamic event binding

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.

Nov 15 '05 #1
1 5661
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

--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Jonathan Yong" <da***********@discussion.com> wrote in message
news:eb**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Pat Richey | last post: by
reply views Thread by Shaul Feldman | last post: by
1 post views Thread by ted benedict | last post: by
1 post views Thread by Tuvas | last post: by
5 posts views Thread by Pupeno | last post: by
reply views Thread by Diogo Bastos | last post: by
1 post views Thread by =?Utf-8?B?QmVu?= | last post: by
9 posts views Thread by mrstevegross | last post: by
1 post views Thread by Albinoswordfish | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.