C# 无法从aspx到cs访问字段ID

C# 无法从aspx到cs访问字段ID,c#,asp.net,visual-studio-2010,C#,Asp.net,Visual Studio 2010,我正在开发一个asp.net web应用程序,我正在尝试构建一个简单的登录页面,其中包含login.aspx和login.aspx.cs aspx页 <%@ Page Title="Log In" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="Login.aspx.cs"

我正在开发一个asp.net web应用程序,我正在尝试构建一个简单的登录页面,其中包含login.aspx和login.aspx.cs

aspx页

<%@ Page Title="Log In" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Login.aspx.cs" Inherits="nikeeWebApplication.Account.Login" culture="auto" meta:resourcekey="PageResource1" uiculture="auto" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   
    <asp:Login ID="LoginUser" runat="server" EnableViewState="False" 
        RenderOuterTable="False" meta:resourcekey="LoginUserResource1">
        <LayoutTemplate>
            <span class="failureNotification">
                <asp:Literal ID="FailureText" runat="server" 
                meta:resourcekey="FailureTextResource1"></asp:Literal>
            </span>
            <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
                 ValidationGroup="LoginUserValidationGroup" 
                meta:resourcekey="LoginUserValidationSummaryResource1"/>
            <div class="accountInfo">
                <fieldset class="login">
                    <legend>LOG IN</legend>
                    <p>
                        <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName" 
                            meta:resourcekey="UserNameLabelResource1">Username:</asp:Label>
                        <asp:TextBox ID="UserName" runat="server" CssClass="textEntry" 
                            meta:resourcekey="UserNameResource1"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                             CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                             ValidationGroup="LoginUserValidationGroup" 
                            meta:resourcekey="UserNameRequiredResource1">*</asp:RequiredFieldValidator>
                    </p>
                    <p>
                        <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password" 
                            meta:resourcekey="PasswordLabelResource1">Password:</asp:Label>
                        <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" 
                            TextMode="Password" meta:resourcekey="PasswordResource1"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                             CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                             ValidationGroup="LoginUserValidationGroup" 
                            meta:resourcekey="PasswordRequiredResource1">*</asp:RequiredFieldValidator>
                    </p>
                    
                </fieldset>
                <p class="submitButton">
                    <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 
                        ValidationGroup="LoginUserValidationGroup" 
                        meta:resourcekey="LoginButtonResource1"/>
                </p>
            </div>
        </LayoutTemplate>
    </asp:Login>
</asp:Content>

          


感谢您的帮助

您需要访问如下文本框:

TextBox Password = LoginUser.FindControl("Password") as TextBox;
TextBox UserName = LoginUser.FindControl("UserName") as TextBox;

cmd.Parameters.AddWithValue("@username", UserName.Text);
cmd.Parameters.AddWithValue("@password", Password.Text);
或者你可以使用:

cmd.Parameters.AddWithValue("@username", LoginUser.UserName);
cmd.Parameters.AddWithValue("@password", LoginUser.Password);
同样,要消除DataTable错误,请添加using语句:

using System.Data;

对于DataTable错误,请使用System.Data添加-
。请在问题中显示所有相关代码,而不是屏幕截图。ASPX页面和codebehind是否在同一命名空间中?对于DataTable错误,请使用System.DataAdd显示完整的标记,因为从外观上看,这是某种类型的模板控件的内部。我认为这是的完全重复。文本框位于
Login
控件内,因此您需要
logiuser.UserName.Text
。不,不需要。如果您有一个ID为
ID
的服务器端控件,那么会为您生成一个ID为该ID的成员。因此,CodeCaster,然后告诉我为什么codebehind看不到这些控件?这是毫无疑问的,因为文本框嵌套在LayoutTemplate中。如果我知道并且可能费心阅读OP的代码并发布答案,我会的。您的回答错误地指出您需要使用
FindControl()
,这很容易被证明是错误的。@WiseGod LoginUser.Password.Text是访问密码文本框内容以及用户名的正确方法。通常使用
FindControl()
是一种设计味道,根本无法解决OP的问题。它提供了一个不必考虑控件层次结构的肮脏解决方法。这就是为什么我投了反对票。您的代码在编辑之前也不起作用,因为
FindControl()
看起来只有一层深度。编辑后,您的答案仍然表明您需要使用
FindControl()
访问文本框,这是错误的。你不需要,有适当的解决办法。
using System.Data;