473,416 Members | 1,607 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 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 5860
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. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.