.aspx中的文本框在.cs中无法访问

本文关键字:访问 cs 文本 aspx | 更新日期: 2023-09-27 18:37:11

我尝试了很多,花了一整天的时间,但都是徒劳的。我发现了许多与我的问题相关的问题,但没有人解决我的问题。我在登录中有一个文本框.aspx ID="用户名"并尝试在登录中获取其值.aspx.cs但它给出了"用户名在当前上下文中不存在"的错误。这是我的登录名.aspx(默认情况下由VS 2012创建)

<%@ Page Title="Log in" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="Login.aspx.cs" Inherits="cpool2.Account.Login" %>  
<%@ Register Src="~/Account/OpenAuthProviders.ascx" TagPrefix="uc" TagName="OpenAuthProviders" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <hgroup class="title">
    <h1><%: Title %>.</h1>
</hgroup>
<section id="loginForm" runat="server">
    <h2>Use a local account to log in.</h2>
    <asp:Login runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
        <LayoutTemplate>
            <p class="validation-summary-errors">
                <asp:Literal runat="server" ID="FailureText" />
            </p>
            <fieldset>
                <legend>Log in Form</legend>
                <ol>
                    <li>
                        <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
                        <asp:TextBox runat="server" ID="UserName"></asp:TextBox>
                        <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName" CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                    </li>
                    <li>
                        <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
                        <asp:TextBox runat="server" ID="Password" TextMode="Password"/>
                        <asp:RequiredFieldValidator runat="server" ControlToValidate="Password" CssClass="field-validation-error" ErrorMessage="The password field is required." />
                    </li>
                    <li>
                        <asp:CheckBox runat="server" ID="RememberMe" />
                        <asp:Label runat="server" AssociatedControlID="RememberMe" CssClass="checkbox">Remember me?</asp:Label>
                        <%--<asp:HyperLink runat="server" ID="RegisterHyperLink" NavigateUrl="~/Account/ForgotPassword.aspx" ViewStateMode="Disabled">Forgot Password?</asp:HyperLink>--%>
                    </li>
                </ol>
                <asp:Button runat="server" CommandName="Login" Text="Log in" OnClick="btnLogin" />
            </fieldset>
        </LayoutTemplate>
    </asp:Login>
    <p>
        <asp:HyperLink runat="server" ID="RegisterHyperLink" ViewStateMode="Disabled">Register</asp:HyperLink>
        if you don't have an account.
    </p>
</section>
</asp:Content>  

登录.aspx.cs如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.AspNet.Membership.OpenAuth;
using System.Text;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Configuration; 
namespace cpool2.Account
{
public partial class Login : System.Web.UI.Page
{
    //protected TextBox UserName;
    protected void Page_Load(object sender, EventArgs e)
    {
        RegisterHyperLink.NavigateUrl = "Register.aspx";
        OpenAuthLogin.ReturnUrl = Request.QueryString["ReturnUrl"];
        var returnUrl = HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        if (!String.IsNullOrEmpty(returnUrl))
        {
            RegisterHyperLink.NavigateUrl += "?ReturnUrl=" + returnUrl;
        }   
    }
    protected void btnLogin(object sender, EventArgs e)
    {
        string abc = UserName.Text; // causing error
    }
  }
}

我正在使用Web应用程序(asp.net 框架4.5)。这可能是我愚蠢的错误,但它让我的一天变得艰难:(

.aspx中的文本框在.cs中无法访问

试试这个:

TextBox UserName = (TextBox)Login1.FindControl("UserName");
if (UserName != null)
{
    string abc = UserName.Text;
}

试试这个。

    first, give id to login control
    <asp:Login id="LoginView1" runat="server" ViewStateMode="Disabled" RenderOuterTable="false">
    </asp:Login>
    then,
    // coding side
    TextBox txt = (TextBox)LoginView1.FindControl("UserName");
    if (txt != null)
    {
        //access here
    }

希望对您有所帮助。
谢谢。

给 asp:login 控件一个 ID,例如 ID="LoginUser"然后你可以像

         LoginUser.UserName.Text

更改此内容

<asp:Login runat="server" ViewStateMode="Disabled" RenderOuterTable="false">

<asp:Login runat="server" id="id_login" RenderOuterTable="false">

并通过以下方式获得价值

id_login.Username

LoginControl分配一个ID

 <asp:Login runat="server" ID="loginForm" ViewStateMode="Disabled" RenderOuterTable="false">  

 protected void btnLogin(object sender, EventArgs e)
        {
            TextBox tbox = this.loginForm.FindControl("UserName") as TextBox;  
            string abc = tbox .Text;
        }