Friday 2 July, 2010

page_load event firing twice in firefox with asp.net page or user control.

We are having a very large project in asp.net 3.5. During code review of project we have found that page_load event of one control is firing twice in the firefox browser. After doing some debugging i have found that it's due to one of the link button which is there in the control. I like to share you the same scenario with you all guys.

One of the developer from us has specified the link button click event two times. The first one in directly in the html with onclick like following.



Code Snippet



  1. <form id="form1" runat="server">
  2. <div>
  3. <asp:LinkButton ID="lnkButton" runat="server"onclick="lnkButton_Click">asp:LinkButton>
  4. div>
  5. form>





And another in the initialize event he has also attached the event handler for the click event like following.



Code Snippet



  1. protected override void OnInit(EventArgs e)
  2. {
  3. lnkButton.Click += new EventHandler(lnkButton_Click);
  4. base.OnInit(e);
  5. }





So i have removed one of them from initialize event and now its calling page_load event only one time. This is also responsible for the failed to load ViewState error some time when you are adding controls dynamically.

No comments:

Post a Comment