简体   繁体   English

从ASP.net页的“登录”控件中获取密码值

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

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. 由于类型为'System.Web.HttpUnhandledException'的异常而引发。

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> 

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 http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.login.aspx

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM