472,368 Members | 2,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,368 software developers and data experts.

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 5759
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Pat Richey | last post by:
i'm trying to create a modular system, and right now i've defined a set of generic events that each module would need to provide definitons for. basically, i want a message to come into the system...
0
by: Shaul Feldman | last post by:
Hello, I have a couple of things: 1) a page contains a button that has some JS code attached to it programmatically. Also the page loads a web user control with LoadControl method. In UC I...
1
by: ted benedict | last post by:
hello everybody, this is my goal: - read out a text input and display it on the page somewhere else... (easy..) and the cache setting is like this: - look for newer versions of stored pages...
1
by: Tuvas | last post by:
I am trying to execute a function with a tkinter event binding double click. With 2 mouse clicks done quickly, the function should happen, otherwise, it should not. However, I am noticing that the...
5
by: Pupeno | last post by:
Hello, I am experiencing a weird behavior that is driving me crazy. I have module called Sensors containing, among other things: class Manager: def getStatus(self): print "getStatus(self=%s)"...
0
by: Diogo Bastos | last post by:
Hello, I'm fairly used to working with Python but it's the first time I'm trying to use Tkinter so I'm running into a problem. I'm using three python scripts with Tkinter GUIs and a fourth...
1
by: =?Utf-8?B?QmVu?= | last post by:
Hi all, I'm reading the book Essential WCF and while trying out the code listing at page 95 I got an ArgumentException when running the code. Here's the code listing: BasicHttpBinding binding...
9
by: mrstevegross | last post by:
I ran into a weird behavior with lexical scope in Python. I'm hoping someone on this forum can explain it to me. Here's the situation: I have an Outer class. In the Outer class, I define a...
1
by: Albinoswordfish | last post by:
Right now I'm having trouble event binding an array of canvases. You see I want to create an array of canvases binding with double click, these binds call a function that take in an argument. ...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.