<" /> <" /> <"/>

Getting the password value from a Login control in ASP.net page

6.5k Views Asked by At

I've got my login control in my site Master page:

<AnonymousTemplate>
    <asp:Login runat="server" OnLoggedIn="Login1_LoggedIn" CssClass="LoginForm" />
</AnonymousTemplate>

In the code-behind page of this Master page I am trying to capture the value entered in the Password form field. The code works in the homepage, but doesn't work in all other pages!

The code used is:

Page page = (Page)HttpContext.Current.Handler;
 TextBox tbtemp = (TextBox)page.FindControl("Password");
 _password = tbtemp.ToString();

On the homepage, looking at the trace the value of the Text box is:

ctl00$LoginView1$ctl01$Password

On the other pages the value is:

ctl00$ctl00$LoginView1$ctl01$Password

The error that is thrown on the non-homepage pages is:

due to Exception of type 'System.Web.HttpUnhandledException' was thrown.

Any ideas how to access the value?

Update:

My login form looks like this:

 <asp:loginview id="LoginView1" runat="server">
                        <LoggedInTemplate >
                            <asp:LoginStatus ID="LoginStatus1" runat="server" OnLoggedOut="LoginStatus1_LoggedOut"  /> <%--Displays the text logged in--%>
                            <asp:LoginName ID="LoginName1" runat="server"  /> <%--displays the username--%>
                        </LoggedInTemplate>
                        <AnonymousTemplate>
                            <asp:Login RememberMeSet="true" ID="loginForm"  runat="server" OnLoggedIn="Login1_LoggedIn"  CssClass="LoginForm" >
                                <LayoutTemplate>
                                <table>
                                <tr>
                                    <td><asp:Label ID="UserNameLabel" runat="server">Username:</asp:Label></td>
                                    <td><asp:TextBox ID="UserName" runat="server" /></td>
                                </tr>
                                <tr>
                                    <td><asp:Label ID="PasswordLabel" runat="server" >Password:</asp:Label></td>
                                    <td><asp:TextBox ID="Password" runat="server" TextMode="Password"  /></td>
                                </tr>
                                <tr>
                                    <td><asp:Label ID="RememberMeLabel" runat="server" >Remember me:&nbsp;</asp:Label></td>
                                    <td><asp:CheckBox ID="RememberMe" runat="server"   /></td>
                                </tr>
                                <tr>
                                    <td>&nbsp;</td>
                                    <td> <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" /></td>
                                </tr>
                                </table>
                                </LayoutTemplate>
                            </asp:Login>
                        </AnonymousTemplate>
             </asp:loginview> 
2

There are 2 best solutions below

0
Subhash Dike On

Why are you getting the password control explicitly? Did you try just getting the password directly from

string password = LoginCtrl.Password //Assuming LoginCtrl is the Id of your control.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.login.aspx

0
matt On
string username = ((Login)this.LoginView.FindControl("LoginControl")).UserName;

 // Search recursively a control sub-tree for a specific control.
        // It searches every control in the sub-tree, so it potentially
        // could be optimized to search only, say, INamingContainers.
        public Control FindControlRecursive(Control root, string id)
        {
            if (root.ID == id) return root;
            foreach (Control c in root.Controls)
            {
                var ctlFound = FindControlRecursive(c, id);
                if (((ctlFound != null))) return ctlFound;
            }
            return null;
        }

        public T FindControl<T>(string id) where T : Control
        {
            return FindControl<T>(Page, id);
        }

        public static T FindControl<T>(Control startingControl, string id) where T : Control
        {
            T found = null;
            foreach (Control activeControl in startingControl.Controls)
            {
                found = activeControl as T;
                if (found == null)
                {
                    found = FindControl<T>(activeControl, id);
                }
                else if (string.Compare(id, found.ID, true) != 0)
                {
                    found = null;
                }
                if (found != null)
                {
                    break;
                }
            }
            return found;
        }

    }