Add Event handlers Issue at Page Load
-
I have following code snippet at page load event. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Button1.Attributes.Add("onclick", "alert('hello')"); Button2.Click += new EventHandler(Button2_Click); } } button1 display the alert message when we click on that. But when i click on button2, it doesn't fire Button2_Click event. Why? Althouh if i comment "if (!IsPostBack)" line, the code working fine.
hello
-
I have following code snippet at page load event. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Button1.Attributes.Add("onclick", "alert('hello')"); Button2.Click += new EventHandler(Button2_Click); } } button1 display the alert message when we click on that. But when i click on button2, it doesn't fire Button2_Click event. Why? Althouh if i comment "if (!IsPostBack)" line, the code working fine.
hello
Arrgggh. This is an ASP.NET question, yet this is PLAINLY NOT an ASP.NET forum? Are you planning on writing an article on this, or did you just randomly click on the links. Anyway, because I'm still in vaguely a good mood, I'll answer it. The simple fact is that a web form is stateless, so you need to reattach the event handlers EACH TIME you post back.
Deja View - the feeling that you've seen this post before.
-
Arrgggh. This is an ASP.NET question, yet this is PLAINLY NOT an ASP.NET forum? Are you planning on writing an article on this, or did you just randomly click on the links. Anyway, because I'm still in vaguely a good mood, I'll answer it. The simple fact is that a web form is stateless, so you need to reattach the event handlers EACH TIME you post back.
Deja View - the feeling that you've seen this post before.
-
shecool wrote:
then why Button1 click event fire without attaching again?
That's not a server side event handler.
Deja View - the feeling that you've seen this post before.
-
I have following code snippet at page load event. protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Button1.Attributes.Add("onclick", "alert('hello')"); Button2.Click += new EventHandler(Button2_Click); } } button1 display the alert message when we click on that. But when i click on button2, it doesn't fire Button2_Click event. Why? Althouh if i comment "if (!IsPostBack)" line, the code working fine.
hello
Wrong forum. For potentially better success in the future, pick the correct forum :|
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
shecool wrote:
then why Button1 click event fire without attaching again?
Because you are not capable of (including to make a good choice of correct technical forum) programming.
Vasudevan Deepak Kumar Personal Homepage
Tech Gossips
All the world's a stage, And all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts... --William Shakespeare