从ASP.net页面的Login控件获取密码值

时间:2011-04-08 09:23:00

标签: asp.net forms loginview

我的网站母版页中有登录控件:

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

在此母版页的代码隐藏页面中,我试图捕获在密码表单字段中输入的值。代码在主页中有效,但在所有其他页面中都不起作用!

使用的代码是:

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

在主页上,查看文本框的值为:

ctl00$LoginView1$ctl01$Password

在其他页面上,值为:

ctl00$ctl00$LoginView1$ctl01$Password

非主页上引发的错误是:

  

由于类型异常   'System.Web.HttpUnhandledException'   被扔了。

有关如何访问该值的任何想法?

更新

我的登录表单如下:

 <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 个答案:

答案 0 :(得分:2)

为什么要明确地获得密码控制?您是否尝试直接从

获取密码
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

答案 1 :(得分:1)

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;
        }

    }