Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Page_Load event executing twice, after Login

Posted on 2008-06-23
10
Medium Priority
?
702 Views
Last Modified: 2010-04-21
Greetings all

I have a somewhat strange scenario. I have a page with a login control on it. in my Page_Load event I am calling a small routine tahts ets various button states. I placed a breakpoint within that routine, in order to do some checking. What I noticed was that after entering user name and password and clicking the LogIn button, my breakpoint was reached twice!

Any explanations?
<%@ Page Language="C#" MasterPageFile="~/Scribble01.master" AutoEventWireup="true" CodeFile="Members.aspx.cs" Inherits="Pages_Member_Members" Title="Member Page" %>
<%@ MasterType VirtualPath="~/Scribble01.master" %>
 
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 
<div id = "membershipTop">
 
    <div id="memberLog">
    
        <center>
        <asp:LoginStatus ID="loginMember" runat="server" CssClass="loginStatus" OnLoggedOut="loginMember_LoggedOut" />       &nbsp;<!-- Start: Login control (converted to Template) -->    
        
       <asp:Login ID="logMember" runat="server" CssClass="login" TitleText="Membership Login"
        FailureText="Login attempt was unsuccessful."  UserNameLabelText="Member:"
        PasswordLabelText= "Password:" UserNameRequiredErrorMessage="Member is required." >
        <TitleTextStyle BackColor="CornflowerBlue" Font-Bold="True" />
     <LayoutTemplate>
         <table border="0" cellpadding="1" cellspacing="0" style="border-collapse: collapse">
             <tr>
                 <td>
                     <table border="0" cellpadding="0">
                         <tr>
                             <td align="center" colspan="2" style="font-weight: bold; background-color: cornflowerblue">
                                 Membership Login</td>
                         </tr>
                         <tr>
                             <td align="right">
                                 <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Member:</asp:Label></td>
                             <td>
                                 <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                     ErrorMessage="Member is required." ToolTip="Member is required." ValidationGroup="logDefault">*</asp:RequiredFieldValidator>
                             </td>
                         </tr>
                         <tr>
                             <td align="right">
                                 <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label></td>
                             <td>
                                 <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
                                 <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
                                     ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="logDefault">*</asp:RequiredFieldValidator>
                             </td>
                         </tr>
                         <tr>
                             <td colspan="2">
                                 <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
                             </td>
                         </tr>
                         <tr>
                             <td align="center" colspan="2" style="color: red">
                                 <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                             </td>
                         </tr>
                         <tr>
                             <td align="right" colspan="2">
                                 <asp:Button ID="btnForgot" runat="server" Text="Forgot Password" ValidationGroup="logForgot" OnClick="btnForgot_Click" />
                                 <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="logDefault" />
                             </td>
                         </tr>
                     </table>
                 </td>
             </tr>
         </table>
     </LayoutTemplate>
    </asp:Login>
        <!-- End: Login control (converted to Template) -->    
        </center>    
    
    <!-- end <div id="memberLog"> -->    
    </div>
    
    <asp:LoginView ID="logViewTop" runat="server"  >        
    
    <AnonymousTemplate>
    <div style ="position: relative; top: opx; width: 76%;">
        <h3>Welcome to our membership page!</h3>
        <p>Please complete the membership application, below, as completely as possible, before submitting.</p>
        <p>Applications are reviewed on a weekly basis. You will be notified via email (sent to the address provided in your application - please ensure that this is correct).</p>
        <p class="smallerFontSize">Note: Membership decisions are, at all times, the prerogative of the committee, whose decision shall be final.</p>              
    </div>
    </AnonymousTemplate>
            
    <LoggedInTemplate>
    <div style ="position: relative; top: opx; width: 76%;">    
        <h3>Welcome!</h3>
        <p>As a logged in member you can use this page to:</p>
        <ul id="memberOptions" type="circle">
            <li>Change your password</li>
            <li>Change your email address</li>
        </ul>
    </div>
    </LoggedInTemplate>
    
    </asp:LoginView>
    <!-- End <LoginView id="logViewTop"> -->
     
<!-- end <div id="membershipTop"> -->        
</div>
 
    
<div id = "membershipBody">
 
     <asp:LoginView ID="logViewBody" runat="server" >
     
        <AnonymousTemplate>
        <center>
        <asp:Panel ID="pnlNonMembership" runat="server" CssClass="membershipPanel" Width="85%">
    
        <!-- Table to sort out the membership application info. -->
        <table id="membershipApp" style="position: relative" >
                <tr>            
                <!-- Row: Name -->
                <td>
	            	<asp:Label ID="lblName" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtName">Name:</asp:Label>
	            </td>
                <td colspan="4">
                    <asp:TextBox ID="txtName" runat="server" Width="140px"></asp:TextBox>		
                    <asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtName"
                        ErrorMessage="*" SetFocusOnError="True" Style="position: relative" ValidationGroup="UserApply"></asp:RequiredFieldValidator></td>            
    	    <!-- End: Row: Name -->
            </tr>
        
    	    <tr>	    
	        <!-- Row: Surname  -->
                <td>
                    <asp:Label ID="lblSurname" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtSurname">Surame:</asp:Label>
                </td>
                <td colspan="4">
                <asp:TextBox ID="txtSurname" runat="server" Width="180px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="rfvSurname" runat="server" ControlToValidate="txtSurname"
                        ErrorMessage="*" SetFocusOnError="True" Style="position: relative" ValidationGroup="UserApply"></asp:RequiredFieldValidator></td>            
    	    <!-- End: Row: Surname -->
            </tr>
       
            <tr>        
        	<!-- Row: Blank -->
                <td colspan="5" height="12"></td>
            </tr>
        
            <tr>                
    	    <!-- Row: DOB -->
                <td>
		            <asp:Label ID="lblDob" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtDob">BirthDate:</asp:Label>
                </td>
                <td colspan="4">
	    	        <asp:TextBox ID="txtDob" runat="server" Width="75px"></asp:TextBox>
                    <ajaxToolkit:MaskedEditExtender ID="MaskedEditExtender1" runat="server" Mask="9999/99/99" MaskType="Date" CultureName="en-za" TargetControlID="txtDob">
                    </ajaxToolkit:MaskedEditExtender>
                    <ajaxToolkit:MaskedEditValidator ID="MaskedEditValidator1" runat="server" ControlExtender="MaskedEditExtender1"
                        ControlToValidate="txtDob" EmptyValueMessage="Required" InvalidValueMessage="Wrong" 
                        IsValidEmpty="False" ></ajaxToolkit:MaskedEditValidator><!-- End: Row: DOB --></tr>
 
            <tr>                
	        <!-- Row: Blank -->
                <td colspan="5" height="12"></td>
	        </tr>
 
            <tr>        
	        <!-- Row: User1 -->
                <td>
		            <asp:Label ID="lblUser1" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtUser1">Username (preferred):</asp:Label>
                </td>
                <td colspan="4">
		            <asp:TextBox ID="txtUser1" runat="server" Width="120px"></asp:TextBox>
		            <asp:RequiredFieldValidator ID="rfvUser1" runat="server" ControlToValidate="txtUser1"
                        ErrorMessage="*" SetFocusOnError="True" Style="position: relative" ValidationGroup="UserApply"></asp:RequiredFieldValidator></td>            
	        <!-- End: Row" User1 -->
            </tr>
 
            <tr>        
	        <!-- Row: User2 -->
                <td>
    		<asp:Label ID="lblUser2" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtUser2">Username (alternate):</asp:Label>
                </td>
                <td colspan="4">
		            <asp:TextBox ID="txtUser2" runat="server" Width="120px"></asp:TextBox>
		            <asp:RequiredFieldValidator ID="rfvUser2" runat="server" ControlToValidate="txtUser2"
                        ErrorMessage="*" SetFocusOnError="True" Style="position: relative" ValidationGroup="UserApply"></asp:RequiredFieldValidator></td>            
	        <!-- End: Row: User2 -->
            </tr>
        
            <tr>                
	        <!-- Row: Blank  -->
                <td colspan="5" height="12"></td>
            </tr>
        
            <tr>                
	        <!-- Row: Email -->
                <td>
		            <asp:Label ID="lblEmail" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtEmail">Email:</asp:Label>
                </td>
                <td colspan="4">
	    	        <asp:TextBox ID="txtEmail" runat="server" Width="200px"></asp:TextBox>
	    	        <asp:RequiredFieldValidator ID="rfvEmail" runat="server" ControlToValidate="txtEmail"
                        ErrorMessage="*" SetFocusOnError="True" Style="position: relative" ValidationGroup="UserApply"></asp:RequiredFieldValidator></td>            
    	    <!-- End: Row: Email -->
            </tr>
        
            <tr>                
	        <!-- Row: ConfirmEmail -->
                <td>
		            <asp:Label ID="lblConfEmail" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtConfEmail">Confirm Email:</asp:Label>
                </td>
                <td colspan="4">
		            <asp:TextBox ID="txtConfEmail" runat="server" Width="200px" Style="position: relative"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvConfEmail" runat="server" ControlToValidate="txtConfEmail"
                        ErrorMessage="*" SetFocusOnError="True" ValidationGroup="UserApply"></asp:RequiredFieldValidator>
                    <asp:CompareValidator ID="cmpvEmail" runat="server" ControlToCompare="txtEmail" ControlToValidate="txtConfEmail"
                        ErrorMessage="Must match email!" SetFocusOnError="True" Style="position: relative"
                        ValidationGroup="UserApply"></asp:CompareValidator></td>            		                                
    	    <!-- End: Row: ConfirmEmail -->
            </tr>
        
            <tr>                
	        <!-- Row: Blank -->
                <td colspan="5" height="12"></td>
            </tr>
        
            <tr>                
	        <!-- Row: Message -->
                <td>
    		        <asp:Label ID="lblMessage" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtMessage">Message:</asp:Label>
                </td>
                <td colspan="4">
		            <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Width="620px" Height="200px"></asp:TextBox>
                </td>
	        <!-- End: Row: Message -->
            </tr>
 
            <tr>                
	        <!-- Row: Blank  -->
                <td colspan="5" height="12"></td>
            </tr>
              
            <tr>                
	        <!-- Row: Sample -->
                <td>
		            <asp:Label ID="lblSample" runat="server" CssClass="smallerFontSize" AssociatedControlID="txtSample">Sample of your writing:</asp:Label>
                </td>
                <td>
	    	        <asp:TextBox ID="txtSample" runat="server" TextMode="MultiLine" Width="620px" Height="350px"></asp:TextBox>
	    	        <asp:RequiredFieldValidator ID="rfvSample" runat="server" ControlToValidate="txtSample"
                        ErrorMessage="*" SetFocusOnError="True" Style="position: relative" ValidationGroup="UserApply"></asp:RequiredFieldValidator></td>            
                <td colspan="3">
                    <center>
                   <asp:FileUpload id="uplSelect" runat="server" Width="140px"  />
                    <asp:Button ID="btnUpLoad" runat="server" Text="Upload text file" Tooltip="Currently only text files accepted"/>&nbsp;
                    <br/>           
                    <br/>           
                    <asp:Button ID="btnClear" runat="server" Text="Clear sample" ToolTip="Clears all text from the sample"/>                
                    </center>
                </td>            
	        <!-- End: Row: Sample -->
            </tr>              
              
            <tr>                
	        <!-- Row: Blank -->
                <td colspan="5" style="height: 10px"></td>
            </tr>
    
        <!-- End: table id="membershipApp". -->
        </table>     
    
        <asp:Button ID="btnSubmitApp" runat="server" Text="Submit Application" ValidationGroup="UserApply" Font-Bold="True" Font-Size="Larger" ForeColor="Red" />
    
        </asp:Panel>
 
     
        </center>       
        <!-- end - AnonymousTemplate for LoginView: logViewBody -->
        </AnonymousTemplate>                        
        
        <LoggedInTemplate>        
 
        <center>
        <asp:Panel ID="pnlMembership" runat="server" CssClass="membershipPanel" Width="400px">
 
        <table id="tblLoggedIn"   style="position: relative">
            <tr>
                <td style="width: 110px">
                <asp:Label ID="lblUser" runat="server" CssClass="smallerFontSize" >Username: </asp:Label>
                </td>
                <td style="width: 240px">
                <asp:Label ID="lblUsername" runat="server" CssClass="labelHighlighted"></asp:Label>
                </td>
            </tr>
            <tr>
                <td colspan="2" height="12px"></td>
            </tr>            
            <tr>
                <td style="width: 110px; height: 30px;">
                <asp:Label ID="lblPassword" runat="server" AssociatedControlId="txtPassword" CssClass="smallerFontSize" >New password: </asp:Label>
                </td>
                <td style="width: 240px; height: 30px;">
                <asp:TextBox ID="txtPassword" runat="server" Width="110px" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 110px">
                <asp:Label ID="lblPassConf" runat="server" AssociatedControlId="txtPassConfirm" CssClass="smallerFontSize">Comfirm password:</asp:Label>
                </td>
                <td style="width: 240px">
                <asp:TextBox ID="txtPassConfirm" runat="server" Width="110px" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td colspan="2" height="12px"></td>
            </tr>            
            <tr>
                <td style="width: 110px">
                <asp:Label ID="lblEmail" runat="server" AssociatedControlId="txtEmail" CssClass="smallerFontSize">Email: </asp:Label>
                </td>
                <td style="width: 240px">
                <asp:TextBox ID="txtEmail" runat="server" Width="240px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td style="width: 110px">
                <asp:Label ID="lblConfEmail" runat="server" AssociatedControlId="lblConfEmail" CssClass="smallerFontSize">Confirm Email: </asp:Label>
                </td>
                <td style="width: 240px">
                <asp:TextBox ID="txtConfirmEmail" runat="server" Width="240px"></asp:TextBox>
                </td>
            </tr>
            <tr>
            <!-- Row: Blank -->
            <td colspan="2" style="height: 10px"></td>
            </tr>
        </table>
        
        <asp:Button ID="btnSubmitChanges" runat="server" Text="Submit Changes" Font-Bold="True" Font-Size="Larger" ForeColor="Red"  />        
        
        </asp:Panel>
        </center>
        
         <!-- end - LoggedInTemplate for LoginView: logViewBody -->
        </LoggedInTemplate>
     
 
     </asp:LoginView>
     <!-- End <LoginView id="logViewBody"> -->     
     
 
 
<!-- end <div id="membershipTop"> -->        
</div>
 
</asp:Content>

Open in new window

0
Comment
Question by:allanmark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 21850293
Do you put your routine in a block like this:
If Not IsPostBack Then
   ' Your small routine
End If

In C#
if (!IsPostBack)
{
    // Your small routine
}
0
 
LVL 16

Expert Comment

by:ororiole
ID: 21851492
It could be that the page load event handler got registered twice. Set autoeventwireup=false in your Page directive. Then rerun the program again. If it hits your breakpoint only once, that was the problem. Somewhere it got registered explicity, then autoeventwireup registered it again.
0
 

Author Comment

by:allanmark
ID: 21852872
Thanks 4 the feedback.

The code is not under any "if (!Page ....)  - some of the struff is needed regardless of the which.

I tried the auoeventwireup -set it to false -- 2 things happened -- (1) Page layout is screwed up (appears that the csss isn't loaded) and (2) the breakpoint (previosuly reached twice) is never reached.

In breaking where I did (see snippet),  after setting autoeventwireup back to true, I noticed that on the first "pass", the user is not authenticated (btnSubmitChangesX   is null) but on the 2nd "pass" the user is (btnSubmitChangesX = ....)

In the snippe
  protected void Page_Load(object sender, EventArgs e)
    {       
       PagePreparations();
    }
 
protected void PagePreparations()
    {
      .......................
      CreateControlVariables();
    }
 
protected void CreateControlVariables()
    {
 
        btnForgotX = (Button)logMember.FindControl("btnForgot");
 
        if (User.Identity.IsAuthenticated)
        {
            btnSubmitChangesX = (Button)logViewBody.FindControl("btnSubmitChanges");
            btnForgotX.Enabled = false;
        }
        else
        {
            btnUpLoadX = (Button)logViewBody.FindControl("btnUpLoad");
            btnClearX = (Button)logViewBody.FindControl("btnClear");
            btnSubmitAppX = (Button)logViewBody.FindControl("btnSubmitApp");
            btnForgotX.Enabled = true;
            
        }
 
        int t = 0;    //  HERE IS WHERE WE ARE BREAKING

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:allanmark
ID: 21852886
Just did a check on the Postback (set breakpoints).  When I click the "Login" button, the first load is a PostBack, but the second load is not.
0
 
LVL 16

Assisted Solution

by:ororiole
ororiole earned 500 total points
ID: 21854526
Ok, my bad. In this case Its working as its supposed to. The login button causes a postback, the page loads and processes its lifecycle. When it gets to processing the events, it sees that a login event was requested. It processes the login (authenticates the user) and then reloads the page to ensure the code for authenticated users is run.
0
 

Author Comment

by:allanmark
ID: 21854679
OK.

So, bottom line = everything is as it should be, and all I need to do is keep that in mind when I code.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21855422
Like I said in my last post, you need to put your routine in the "If not  postback " block if you don't want it to be executed more than once, otherwise, any posttack will cause the execution of the routine.

The reason the Page_Load was hit twice is that the first hit is caused by the button click's postback, and the second one is caused by the return of the authenticated request.
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21885092
If everything is fine, please close the question.

Thanks,
0
 

Author Comment

by:allanmark
ID: 21885380
One humblest apologies - it completely slipped my mind.

Will close it forthwith.
0
 

Author Closing Comment

by:allanmark
ID: 31469941
Many thanks!!! Again, my Apologies for forgetting to close!
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question