0

我无法在 LoginView 内的登录控件中获取用户名文本框。它告诉我 UName “对象未设置为对象的实例”

Dim UName As TextBox = CType(LoginView1.FindControl("Login1").FindControl("UserName"), TextBox)

aspx

<asp:LoginView ID="LoginView1" runat="server">
            <LoggedInTemplate>
                <div class="logged">                        
                    <ul class="Logged">
                        <li><asp:LoginStatus ID="LoginStatus1" Font-Size="Small" LogoutPageUrl="http://NCOASouth.com" LogoutAction="Redirect" runat="server" /></li>
                        <li><asp:HyperLink ID="AdminLink" Visible="false" runat="server" NavigateUrl="~/Admin/Default.aspx" Text="Administration"></asp:HyperLink></li>
                        <li>Welcome, <asp:LoginName ID="LoginName1" runat="server" />!</li>

                    </ul>
                </div>


            </LoggedInTemplate>
            <AnonymousTemplate>
                <h2 class="title1">Officials Login</h2>             
                    <br /><br /><br />
                    <asp:Login ID="Login1" DestinationPageUrl="http://NCOASouth.com/redirect.aspx"
                                CreateUserText="create new account" CreateUserUrl="~/Register.aspx" runat="server">
                                <LayoutTemplate>
                                    <asp:TextBox ID="UserName" CssClass="textbox" runat="server" Width="195px" placeholder="Username"></asp:TextBox><br />
                                    <asp:TextBox ID="Password" CssClass="textbox" runat="server" TextMode="Password" Width="195px" placeholder="Password"></asp:TextBox><br /><br />
                                    <asp:Button ID="LoginButton" ValidationGroup="ctl00$Login1" CssClass="textbox" runat="server" CommandName="Login" Width="200px" Text="Sign In"/>
                                    <br /><br />
                                    <asp:HyperLink ID="createnewuser" runat="server" NavigateUrl="~/Register.aspx" Text="Register for Free" /><br />
                                    <asp:HyperLink ID="forgotpassword" runat="server" NavigateUrl="~/forgotPassword.aspx" Text="Forgot Password" />

                                    <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                                    <br />
                                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server"
                                        ControlToValidate="UserName" ErrorMessage="User Name is required."
                                        ToolTip="User Name is required." ValidationGroup="ctl00$Login1"></asp:RequiredFieldValidator>
                                    <br />
                                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server"
                                        ControlToValidate="Password" ErrorMessage="Password is required."
                                        ToolTip="Password is required." ValidationGroup="ctl00$Login1"></asp:RequiredFieldValidator>
                                </LayoutTemplate>
                            </asp:Login>
            </AnonymousTemplate>
        </asp:LoginView>
4

1 回答 1

0

这是在 C# 中,但您可以使用与此类似的内容来找到您的控件:

http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx

通常,FindControl除非您只想深入一层,否则很少有用。

编辑:

从提供的链接:

public static T FindControl<T>(System.Web.UI.ControlCollection Controls, string ControlName) where T : class
{
     T found = default(T);

     if (Controls != null && Controls.Count > 0)
     {
          for (int i = 0; i < Controls.Count; i++)
          {
               if (Controls[i] is T && Controls[i].ID == ControlName)
               {
                    found = Controls[i] as T;
                    break;
               }
               else
                    found = FindControl<T>(Controls[i].Controls);
          }
     }

     return found;
}

在您的情况下,您将使用(在 C# 中):

var uName = FindControl<TextBox>(LoginView1.Controls, "UserName");

VB.NET 中的泛型用法不是我非常熟悉的。您可以删除它的泛型部分并使用Control.

于 2013-09-23T17:57:44.357 回答