Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 716
  • Last Modified:

Page_Load event executing twice, after Login

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
allanmark
Asked:
allanmark
  • 5
  • 3
  • 2
2 Solutions
 
prairiedogCommented:
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
 
ororioleCommented:
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
 
allanmarkAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
allanmarkAuthor Commented:
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
 
ororioleCommented:
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
 
allanmarkAuthor Commented:
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
 
prairiedogCommented:
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
 
prairiedogCommented:
If everything is fine, please close the question.

Thanks,
0
 
allanmarkAuthor Commented:
One humblest apologies - it completely slipped my mind.

Will close it forthwith.
0
 
allanmarkAuthor Commented:
Many thanks!!! Again, my Apologies for forgetting to close!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now