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.
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.