Add Event handlers Issue at Page Load

Posted by Webmaster under Others on 9/6/2008 | Views : 2567 | Status : [Administrator] | Replies : 1
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.

Posted on behalf of Gehlot.arvind

Best regards,


Posted by: Bhadelia.imran on: 9/7/2008 [Member] Starter

You are adding event on not IsPostBack, so when postback occured that event will not attached to the button. So If you need that event every time, remove the IsPostBack. And put that code in Init method, generally all such handlers will be added in init method.

Webmaster, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response