Asked by:
Load fires twice.

Question
-
User-542428536 posted
I just noticed that after I migrated my project Load event started firing twice and even code in if(!IsPostBack) fires twice.
It seems to be that the reason is that asp.net 2.0 adds Page_Load automatically for Load event and since in 1.1 version there was line like:
this.Load+=new EventHandler(thi.Page_Load);
it is added second time.
For some reason in my case this line wasn't removed by "Convert to Web Application" command.
Just wanted to make sure that I have to do it myself and that this know bug.
Thank you,
Shimon.
Monday, May 15, 2006 8:53 AM
All replies
-
User-1394831673 posted
Hi Shimon,
Can you tell me what function contains the line
this.Load+=new EventHandler(thi.Page_Load);
Also, is there any user-added code in that function?
Thanks!
Tuesday, May 16, 2006 11:39 AM -
User-542428536 posted
For a example like this
#region
Web Form Designer generated code override protected void OnInit(EventArgs e){
// // CODEGEN: This call is required by the ASP.NET Web Form Designer. //InitializeComponent();
base.OnInit(e);}
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent(){
this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click); this.grdList.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.grdList_ItemDataBound); this.Load += new System.EventHandler(this.Page_Load);}
#endregion
Tuesday, May 16, 2006 1:14 PM -
User-964605352 posted
Check to see if you "AutoEventWireup" set to true. If this is a control, check the parent control/page also. Even if the control itself is set to false, but the parent is set to true, it seems to fire anyway.
We've been changing them from Page_Load to <Control>_Load in our usercontrols just for that reason.
-mkc
Tuesday, May 16, 2006 4:23 PM -
User601971297 posted
ashram99 :
I tried both AutoEventWireup=false (and true) and it made no change.
The page load fires twice on DDL's in an updatepanel. The autopostback=true
because I need to capture the selectedindexchanged event. Even tho in an
updatepanel the DDL causes a postback. if I setautopostback=false the event
is queued and eventually fires when I hit a button which causes a postback, but
that's too late to do what I need to do. Here is the HTML, if it will help.
<%--*** the add new panel/form --%> <asp:Panel ID="Panel3" runat="server" BackColor="PaleGoldenrod" Height="265px" Style="left: 256px; ; top: 352px; display:none;" EnableTheming="False" Width="270px" > <div style="border: 1px outset white; width: 250px; height: 245px; left: 8px; ; top: 8px;" align="left"> <asp:Label ID="lblInMarket" runat="server" EnableTheming="False" EnableViewState="False" Font-Bold="True" ForeColor="Black" Height="16px" Style="font-size: 8pt; left: 0.1in; color: black; font-family: Tahoma; ; top: 0.18in" Text="In Market:" Width="64px"></asp:Label> <asp:UpdatePanel ID="UpdatePanel1" ChildrenAsTriggers=true runat="server"> <ContentTemplate> <asp:Label ID="lblSearchBy" runat="server" Font-Bold="True" ForeColor="Black" Height="16px" Style="font-size: 8pt; left: 0.08in; color: black; font-family: Tahoma; ; top: 0.47in; text-align: right" Text="Search By:" Width="64px"></asp:Label> <asp:Label ID="lblFor" runat="server" Font-Bold="True" ForeColor="Black" Height="16px" Style="font-size: 8pt; left: 0.41in; color: black; font-family: Tahoma; ; top: 0.75in; text-align: right" Text="For:" Width="32px"></asp:Label> <asp:Label ID="lblMessage" runat="server" BackColor="White" BorderColor="#00C000" Font-Bold="True" Style="left: 8px; ; top: 192px; text-align: center;" Text="Signals on new additions to your watch list (when they occur) are available within 24 hours." Width="232px" EnableTheming="False" EnableViewState="False" ForeColor="#C00000"></asp:Label> <asp:DropDownList ID="cmbMarket" runat="server" Style="left: 0.84in; ; top: 0.13in" TabIndex="1" Width="96px" EnableTheming="False" AutoPostBack="True"> </asp:DropDownList> <asp:DropDownList ID="cmbSearchBy" runat="server" Style="left: 0.84in; ; top: 0.42in" TabIndex="1" Width="96px" EnableTheming="False" AutoPostBack="True"> </asp:DropDownList> <asp:TextBox ID="txtSearchFor" runat="server" EnableTheming="False" Height="14px" MaxLength="25" Style="font-size: 13px; left: 0.84in; ; top: 0.71in" TabIndex="2" Width="144px"></asp:TextBox> <asp:TextBox ID="TxtCoName" runat="server" EnableTheming="False" Height="14px" MaxLength="35" ReadOnly="True" Style="font-size: 12px; left: 0.81in; ; top: 1.28in" TabIndex="-1" Width="136px" EnableViewState="False"></asp:TextBox> <asp:ImageButton ID="cmdSearch" runat="server" CausesValidation="False" EnableTheming="False" EnableViewState="False" Height="22px" ImageUrl="~/Images/Static/_Find.jpg" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('cmdSearch','','Images/Static/_FindOver.jpg',1)" Style="left: 80px; ; top: 96px" Width="90px" OnClick="cmdSearch_Click" /> <asp:ImageButton ID="cmdAddPanel3" runat="server" CausesValidation="False" EnableTheming="False" EnableViewState="False" Height="22px" ImageUrl="~/Images/Static/_AddNew.jpg" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('cmdAddNew','','Images/Static/_AddNewOver.jpg',1)" Style="left: 40px; ; top: 160px" Width="90px" OnClick="cmdAddPanel3_Click" /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="cmbMarket" EventName="SelectedIndexChanged" /> <asp:AsyncPostBackTrigger ControlID="cmbSearchBy" EventName="SelectedIndexChanged" /> <asp:AsyncPostBackTrigger ControlID="cmdSearch" EventName="Click" /> </Triggers> </asp:UpdatePanel> <asp:ImageButton ID="cmdAddCancel" runat="server" EnableTheming="False" EnableViewState="False" Height="22px" ImageUrl="~/Images/Static/_AddNewCancel.jpg" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('cmdCancel','','Images/Static/_AddNewCancelOver.jpg',1)" Style="left: 144px; ; top: 160px" Width="90px" OnClick="cmdAddCancel_Click" CausesValidation="False" /> </div> </asp:Panel> <AjaxControlToolkit:ModalPopupExtender ID="ModalPopupExtender3" runat="server" BackgroundCssClass="modalBackground" TargetControlID="cmdAddnew" PopupControlID="Panel3"> </AjaxControlToolkit:ModalPopupExtender>
Thanks for any help..
Saturday, April 21, 2007 2:32 PM -
User1567897397 posted
Yes, you will need autopostback=true if you want the postback to occur immediately the event occurs in the UI.
If you are using autoeventwireup=true then make sure that you are not manually hooking up the event handlers in the code behind as well, e.g. cmdAddPanel3.Click += new ImageClickEventHandler(cmdAddPanel3_Click);
If you could post the entire web form and code behind file then I could test it out here and see if I get the same problem.
Monday, April 23, 2007 4:42 PM -
User1299367673 posted
I'm having the same problem and in Visual Studio 2005 the event handlers in the code behind are hidden or non existant, I can't seem to find it how do I look it up. If how do I get them to show up.Friday, April 27, 2007 11:23 AM -
User-1049287122 posted
Somehow my page_load's were changed to below , so that was the issue, took off second loads and now it s goodPrivate Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
Monday, October 29, 2007 5:30 PM -
User-1049287122 posted
Whenever I add a WARP Infragistics control and set the triggercontrolids property it changes the source code, so those infragistics users watch out, when you set that
Monday, October 29, 2007 5:48 PM -
User-1335903275 posted
I know this is an older topic but in case anyone encounters the same issue, I just wanted to share the solution to my particular situation for this problem:
I had pasted the following "Initializecomponent" code into my solution as part of some 3rd party code I had copied from the internet.
#region Web Form Designer generated codeoverride protected void OnInit(EventArgs e){
// // CODEGEN: This call is required by the ASP.NET Web Form Designer. //InitializeComponent();
base.OnInit(e);}
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent(){
this.Load += new System.EventHandler(this.Page_Load);}
#endregion
The issue was the line beginning with "this.Load". Once I pulled that code out, everything worked fine.
-Goalie35
Wednesday, March 4, 2009 10:00 PM -
User-653955379 posted
I just noticed that after I migrated my project Load event started firing twice and even code in if(!IsPostBack) fires twice.
It seems to be that the reason is that asp.net 2.0 adds Page_Load automatically for Load event and since in 1.1 version there was line like:
this.Load+=new EventHandler(thi.Page_Load);
it is added second time.
For some reason in my case this line wasn't removed by "Convert to Web Application" command.
Just wanted to make sure that I have to do it myself and that this know bug.
Visit this link. You may find solution.
http://forums.asp.net/t/1401763.aspx
Wednesday, March 25, 2009 8:16 AM -
User-553078976 posted
Hello everyone,
I know this is a old post. I faced the same scenario recently when we are trying to migrate our application developed in .NET framework 1.1 to framework 3.5. As Goalie53 told in his recent post, removing the line
"this.load += <pageload event name>" solved the issue.
The reason is that, .NET framework 2.0 introduced the concept of partial classes, which was not available in 1.1. In 1.1 the designer code is also initialized during the page init, so we had to explicitly assign the page load handler to the event. From 2.0, when we design a page, the associated designer file takes care of initializing and loading the page as well as the controls.
May be in your case if you have copied the code from 1.1 application, then the reason might be that the page_load is already handled in designer file and again you are calling in the Initialize component method. This makes the page load twice.
Thanks
Kiran
Friday, July 31, 2009 10:56 AM -
User-952121411 posted
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.LoadThe above line of code was also an issue in one of my web forms, and worked by correcting it to the line below:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
The extra 'MyBase.Load' appeared in a particular page and was causing it to call the initial Page_Load twice. Good catch, thank you.
Tuesday, December 8, 2009 12:42 PM -
User-625807879 posted
Somehow my page_load's were changed to below , so that was the issue, took off second loads and now it s good
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, Me.Load
Thanks this worked for me!! :)
Friday, January 15, 2010 6:24 PM -
User1238825301 posted
This might be of use to someone. My problem was caused because I had added an Inherits statement being a bit confused:
Inherits System.Web.UI.UserControl'Inherits Controls_ctrlVidAssessInherits System.Web.UI.UserControl
(was)
'Inherits Controls_ctrlMyControl
It was my daftness, but created these symptoms, I was messing around with creating events within a sub control.
Wednesday, July 21, 2010 5:10 AM -
User1618198903 posted
hey, many thanks. you helped me to solve my problem.
Monday, October 11, 2010 1:58 AM -
User-390775641 posted
Guys, I know it's an old thread. But what's the solution for C#?
Sunday, July 10, 2011 10:25 PM -
User1310055179 posted
I just ran into the same problem, and I found out that the AjaxcontrolToolkit was causing the double postback to the server.
Thursday, February 9, 2017 8:55 AM