loginview + FindControl not working for me.

Last post 02-28-2007 1:33 PM by bryankia. 7 replies.

Sort Posts:

  • loginview + FindControl not working for me.

    02-28-2007, 10:06 AM
    • Member
      538 point Member
    • bryankia
    • Member since 12-30-2002, 5:00 AM
    • Tulsa
    • Posts 125

    I need to make the user select a checkbox before I let them log in.  Sounds simple right.  but I am doing something wrong.  here is my code that always returns null for the controls.  Any help would be greatly appreciated.  I have tried this code in the page load, and prerender of the login control.

    //Get the Check Box
            CheckBox cb = (CheckBox)MasterLoginView.FindControl("ValidateCheckBox") as CheckBox;
           
            // Check to make sure the check box is there.
            if (cb != null)
            {
                // Get the login button. Button btn = (Button)MasterLoginView.FindControl("LoginButton") as Button;
               
                // Check to make sure the button is there.
                if (btn != null)
                {
                    // if the checkbox is checked then enable the button.
                    btn.Enabled = cb.Checked;               
                }
            }

     
  • Re: loginview + FindControl not working for me.

    02-28-2007, 11:34 AM
    • All-Star
      86,754 point All-Star
    • ecbruck
    • Member since 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator

    How about doing something like this?

    ASPX 

    <asp:loginview id="LoginView1" runat="server">
    	<anonymoustemplate>
    		<asp:checkbox id="chkConfirm" runat="server" autopostback="true" oncheckedchanged="chkConfirm_CheckedChanged"
    			text="You must check this box before logging in" /><br />
    		<br />
    		<asp:button id="btnLogin" runat="server" enabled="false" text="Login" />
    	</anonymoustemplate>
    </asp:loginview>

    CODE-BEHIND 

    protected void chkConfirm_CheckedChanged(object sender, EventArgs e)
    {
    	// Retrieve controls
    	CheckBox chkConfirm = sender as CheckBox;
    	Button btnLogin = LoginView1.FindControl("btnLogin") as Button;
    
    	if (btnLogin != null)
    	{
    		btnLogin.Enabled = chkConfirm.Checked;
    	}
    }

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: loginview + FindControl not working for me.

    02-28-2007, 12:02 PM
    • Member
      538 point Member
    • bryankia
    • Member since 12-30-2002, 5:00 AM
    • Tulsa
    • Posts 125

    Almost works for me

    Thanks.  that resolved half my problem! 

    I am still getting a null for the login button.  By accessing the check box through the sender I can reference it.  But I am still a no go on the button.

     Here is the code for accessing the button.

    Button btn = MasterLoginView.FindControl("LoginButton") as Button

    I have checked the spelling several times and even copied the ID value from the control into the code so I  know the spelling is correct. 

     

  • Re: loginview + FindControl not working for me.

    02-28-2007, 12:42 PM
    • All-Star
      86,754 point All-Star
    • ecbruck
    • Member since 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator
    If the Button is within the same template as the CheckBox, then the FindControl method should work. Are you trying to access a control from another template by chance?
    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: loginview + FindControl not working for me.

    02-28-2007, 12:51 PM
    • Member
      538 point Member
    • bryankia
    • Member since 12-30-2002, 5:00 AM
    • Tulsa
    • Posts 125

    That is the crazy part.  I am not.   Below is my code behind and the script.  I just know I have missed something incredibly stupid because rocket science this is not!

     thanks for taking the time to help me with this.

    <asp:LoginView ID="MasterLoginView" runat="server">
                <LoggedInTemplate>
                     <br />
                    <br />
                    <br />
                    <br />
                    <br />
                    <br />
                    Welcome Back
                    <asp:LoginName ID="LoginName1" runat="server" />
                    <br />
                    <br />
                    <br />
                    <br />
                    <br />
                    <br />
                    <br />
                    <br />
                    <br />
                    <br />
                    <br />
                </LoggedInTemplate>
                <AnonymousTemplate>
                    <asp:Login ID="Login1" runat="server" BackColor="#F7F7DE" BorderColor="#CCCC99" BorderStyle="Solid"
                        BorderWidth="1px" Font-Names="Verdana" Font-Size="10pt" Orientation="Horizontal"
                        PasswordRecoveryText="Forgot your Password?" PasswordRecoveryUrl="~/USER/RecoverPassword.aspx">
                        <TitleTextStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                        <LayoutTemplate>
                            <table id="TABLE1" border="0" cellpadding="1" cellspacing="0" style="border-collapse: collapse"
                                onclick="return TABLE1_onclick()">
                                <tr>
                                    <td>
                                        <table border="0" cellpadding="0">
                                            <tr>
                                                <td align="center" colspan="6" style="font-weight: bold; color: white; background-color: #6b696b">
                                                    Log In</td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label></td>
                                                <td>
                                                    <asp:TextBox ID="UserName" runat="server"></asp:TextBox>
                                                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
                                                        ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="ctl00$Login1">*</asp:RequiredFieldValidator>
                                                </td>
                                                <td>
                                                    <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="ctl00$Login1">*</asp:RequiredFieldValidator>
                                                </td>
                                                <td>
                                                    <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
                                                </td>
                                                <td>
                                                    <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="ctl00$Login1" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="6" style="color: red">
                                                    <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td colspan="6">
                                                    <asp:HyperLink ID="PasswordRecoveryLink" runat="server" NavigateUrl="~/USER/RecoverPassword.aspx">Forgot your Password?</asp:HyperLink>
                                                </td>
                                            </tr>
                                        </table>
                                        <asp:CheckBox ID="ValidateCheckBox" runat="server" AutoPostBack="True" Text="By selecting this check box you are acknowledging that the account you are using to log into this system was issued to you."
                                            TextAlign="Left" OnCheckedChanged="ValidateCheckBox_CheckedChanged" Visible="False" /></td>
                                </tr>
                            </table>
                        </LayoutTemplate>
                    </asp:Login>
                </AnonymousTemplate>
            </asp:LoginView>
      
    protected void ValidateCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            // Check to make sure the check box is there.
            if (cb != null)
            {
                // Get the login button.
                Button btn = MasterLoginView.FindControl("LoginButton") as Button;
    
                // Check to make sure the button is there.
                if (btn != null)
                {
                    // if the checkbox is checked then enable the button.
                    btn.Enabled = cb.Checked;
                }
            }
        }    
     
  • Re: loginview + FindControl not working for me.

    02-28-2007, 12:58 PM
    • Member
      313 point Member
    • LuisE
    • Member since 06-08-2005, 3:08 AM
    • Weston, FL
    • Posts 89

    I do not know why but if you have a LoginView the FindControl will not work.

    If you do:

    MasterLoginView.Controls[0].FindControl("LoginButton");

    it will work.

    -Luis 

    just a kid
  • Re: loginview + FindControl not working for me.

    02-28-2007, 1:17 PM
    Answer
    • All-Star
      86,754 point All-Star
    • ecbruck
    • Member since 12-30-2005, 2:39 PM
    • Des Moines, IA
    • Posts 9,209
    • Moderator

    Your problem is that your attempting to find a control within a template within a template. Therefore, you need to find the Login control first, then find the Button like so:

    ASPX 

    <asp:loginview id="LoginView1" runat="server">
    	<anonymoustemplate>
    		<asp:login id="Login1" runat="server">
    			<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">
    										Log In</td>
    								</tr>
    								<tr>
    									<td align="right">
    										<asp:label id="UserNameLabel" runat="server" associatedcontrolid="UserName">User Name:</asp:label></td>
    									<td>
    										<asp:textbox id="UserName" runat="server" />
    										<asp:requiredfieldvalidator id="UserNameRequired" runat="server" controltovalidate="UserName"
    											errormessage="User Name is required." tooltip="User Name is required." validationgroup="ctl00$Login1">*</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:requiredfieldvalidator id="PasswordRequired" runat="server" controltovalidate="Password"
    											errormessage="Password is required." tooltip="Password is required." validationgroup="ctl00$Login1">*</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" />
    									</td>
    								</tr>
    								<tr>
    									<td align="right" colspan="2">
    										<asp:button id="LoginButton" runat="server" commandname="Login" enabled="False" text="Log In"
    											validationgroup="ctl00$Login1" />
    									</td>
    								</tr>
    							</table>
    							<asp:checkbox id="ValidateCheckBox" runat="server" autopostback="True" oncheckedchanged="ValidateCheckBox_CheckedChanged"
    								text="By selecting this check box you are acknowledging that the account you are using to log into this system was issued to you." />
    						</td>
    					</tr>
    				</table>
    			</layouttemplate>
    		</asp:login>
    	</anonymoustemplate>
    </asp:loginview>

    CODE-BEHIND 

    protected void ValidateCheckBox_CheckedChanged(object sender, EventArgs e)
    {
    	// Retrieve controls
    	CheckBox chk = sender as CheckBox;
    	Login log = LoginView1.FindControl("Login1") as Login;
    
    	if (log != null)
    	{
    		Button btn = log.FindControl("LoginButton") as Button;
    
    		if (btn != null)
    		{
    			btn.Enabled = chk.Checked;
    		}
    	}
    }

    Thanks, Ed

    Microsoft MVP - ASP/ASP.NET

  • Re: loginview + FindControl not working for me.

    02-28-2007, 1:33 PM
    • Member
      538 point Member
    • bryankia
    • Member since 12-30-2002, 5:00 AM
    • Tulsa
    • Posts 125
    Well when you put it that way it makes perfect sense.  Thank you very much that worked like a charm.
Page 1 of 1 (8 items)