从asp.net读取文本框值并将其传递给c#变量

从asp.net读取文本框值并将其传递给c#变量,c#,asp.net,visual-studio-2012,C#,Asp.net,Visual Studio 2012,我有一个带有表单I的页面,当我填写表单时,我希望访问我的c#codebehind文件中的表单数据 注册。aspx: <%@ Page Title="Home" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="Register.aspx.cs" Inherits="Informito._Default" %> <div> <table class="

我有一个带有表单I的页面,当我填写表单时,我希望访问我的c#codebehind文件中的表单数据

注册。aspx:

<%@ Page Title="Home" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="Register.aspx.cs" Inherits="Informito._Default" %>
<div>
    <table class="style1">
    <tr>
        <td>Utilizador:</td>
        <td><asp:TextBox ID="Utilizador" name="Utilizador" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox></td>
    </tr>
    </table>
</div>
<asp:Button ID="Button1" runat="server" Text="Registar" OnClick="Registar"/>
namespace Informito {


    public partial class _Default {

        protected global::System.Web.UI.WebControls.LoginView LoginView1;
        protected global::System.Web.UI.WebControls.TextBox Utilizador;
        protected global::System.Web.UI.WebControls.TextBox Password;
    }
}
Register.aspx.designer.cs:

<%@ Page Title="Home" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="Register.aspx.cs" Inherits="Informito._Default" %>
<div>
    <table class="style1">
    <tr>
        <td>Utilizador:</td>
        <td><asp:TextBox ID="Utilizador" name="Utilizador" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox></td>
    </tr>
    </table>
</div>
<asp:Button ID="Button1" runat="server" Text="Registar" OnClick="Registar"/>
namespace Informito {


    public partial class _Default {

        protected global::System.Web.UI.WebControls.LoginView LoginView1;
        protected global::System.Web.UI.WebControls.TextBox Utilizador;
        protected global::System.Web.UI.WebControls.TextBox Password;
    }
}

我必须使用什么函数从asp.net文本框中读取并通过c#codebehind变量将其传递给?

请确保您的代码位于
标记中

您的代码应该如下所示:

<form runat='server' id="form1">
<div>
<table class="style1">

<tr>
<td>Utilizador:</td>
<td>
<asp:TextBox ID="Utilizador" name="Utilizador" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Password:</td>
<td><asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
</table>
</div>
<asp:Button ID="Button1" runat="server" Text="Registar" OnClick="Registar"/>
</form>

实用程序:

确保您的代码在
标签中

您的代码应该如下所示:

<form runat='server' id="form1">
<div>
<table class="style1">

<tr>
<td>Utilizador:</td>
<td>
<asp:TextBox ID="Utilizador" name="Utilizador" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>Password:</td>
<td><asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
</table>
</div>
<asp:Button ID="Button1" runat="server" Text="Registar" OnClick="Registar"/>
</form>

实用程序:

更新:

<%@ Page Title="Home" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="Register.aspx.cs" Inherits="Informito._Default" %>
<div>
    <table class="style1">
    <tr>
        <td>Utilizador:</td>
        <td><asp:TextBox ID="Utilizador" name="Utilizador" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox></td>
    </tr>
    </table>
</div>
<asp:Button ID="Button1" runat="server" Text="Registar" OnClick="Registar"/>
namespace Informito {


    public partial class _Default {

        protected global::System.Web.UI.WebControls.LoginView LoginView1;
        protected global::System.Web.UI.WebControls.TextBox Utilizador;
        protected global::System.Web.UI.WebControls.TextBox Password;
    }
}
因为您使用的是母版页,所以需要将标记包含在
标记中


如果使用Visual Studio生成web项目,则可以看到Register.aspx.designer.cs,其代码如下所示。如果没有,则将其添加到类中(可以在Register.aspx.cs或Register.aspx.designer.cs中添加)

然后你可以用

string UserName = Utilizador.Text;

更新:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Data.SqlClient;
using Informito.Admins;

namespace Informito
{
    public partial class _Default : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void Registar(object sender, EventArgs e)
        {
            string Username = Utilizador.Text; //Error here: Object reference not set to an instance of an object.
            string Password= Password.Text;
            RegisterUser(Username, Password, 1);
        }

    }
}
我刚试过这个,似乎效果不错:

Register.aspx.cs

public void Registar(object sender, EventArgs e)
{
    string Username = Utilizador.Text; 
    string PassWd = Password.Text;
    RegisterUser(Username, PassWd, 1);
}
public partial class _Default {

        protected global::System.Web.UI.WebControls.TextBox Utilizador;
        protected global::System.Web.UI.WebControls.Button Button1;
        protected global::System.Web.UI.WebControls.TextBox Password;
    }
Register.aspx.designer.cs

public void Registar(object sender, EventArgs e)
{
    string Username = Utilizador.Text; 
    string PassWd = Password.Text;
    RegisterUser(Username, PassWd, 1);
}
public partial class _Default {

        protected global::System.Web.UI.WebControls.TextBox Utilizador;
        protected global::System.Web.UI.WebControls.Button Button1;
        protected global::System.Web.UI.WebControls.TextBox Password;
    }
Register.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Register.aspx.cs" Inherits="Informito._Default" %>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
    <table class="style1">
    <tr>
        <td>Utilizador:</td>
        <td><asp:TextBox ID="Utilizador" name="Utilizador" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox></td>
    </tr>
    </table>
</div>
<asp:Button ID="Button1" runat="server" Text="Registar" OnClick="Registar"/>
</asp:Content>

实用程序:
密码:

更新:

<%@ Page Title="Home" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="True" CodeBehind="Register.aspx.cs" Inherits="Informito._Default" %>
<div>
    <table class="style1">
    <tr>
        <td>Utilizador:</td>
        <td><asp:TextBox ID="Utilizador" name="Utilizador" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox></td>
    </tr>
    </table>
</div>
<asp:Button ID="Button1" runat="server" Text="Registar" OnClick="Registar"/>
namespace Informito {


    public partial class _Default {

        protected global::System.Web.UI.WebControls.LoginView LoginView1;
        protected global::System.Web.UI.WebControls.TextBox Utilizador;
        protected global::System.Web.UI.WebControls.TextBox Password;
    }
}
因为您使用的是母版页,所以需要将标记包含在
标记中


如果使用Visual Studio生成web项目,则可以看到Register.aspx.designer.cs,其代码如下所示。如果没有,则将其添加到类中(可以在Register.aspx.cs或Register.aspx.designer.cs中添加)

然后你可以用

string UserName = Utilizador.Text;

更新:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Data.SqlClient;
using Informito.Admins;

namespace Informito
{
    public partial class _Default : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void Registar(object sender, EventArgs e)
        {
            string Username = Utilizador.Text; //Error here: Object reference not set to an instance of an object.
            string Password= Password.Text;
            RegisterUser(Username, Password, 1);
        }

    }
}
我刚试过这个,似乎效果不错:

Register.aspx.cs

public void Registar(object sender, EventArgs e)
{
    string Username = Utilizador.Text; 
    string PassWd = Password.Text;
    RegisterUser(Username, PassWd, 1);
}
public partial class _Default {

        protected global::System.Web.UI.WebControls.TextBox Utilizador;
        protected global::System.Web.UI.WebControls.Button Button1;
        protected global::System.Web.UI.WebControls.TextBox Password;
    }
Register.aspx.designer.cs

public void Registar(object sender, EventArgs e)
{
    string Username = Utilizador.Text; 
    string PassWd = Password.Text;
    RegisterUser(Username, PassWd, 1);
}
public partial class _Default {

        protected global::System.Web.UI.WebControls.TextBox Utilizador;
        protected global::System.Web.UI.WebControls.Button Button1;
        protected global::System.Web.UI.WebControls.TextBox Password;
    }
Register.aspx

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Register.aspx.cs" Inherits="Informito._Default" %>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<div>
    <table class="style1">
    <tr>
        <td>Utilizador:</td>
        <td><asp:TextBox ID="Utilizador" name="Utilizador" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Password:</td>
        <td><asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox></td>
    </tr>
    </table>
</div>
<asp:Button ID="Button1" runat="server" Text="Registar" OnClick="Registar"/>
</asp:Content>

实用程序:
密码:


Ex:我有一个名为Register.aspx的asp.net页面和代码隐藏文件Register.aspx.csi在您的aspx页面中,您的控件是否嵌套在带有属性runat=“server”的表单标记中?如果您不将控件嵌套在服务器端表单中,它们将不会在代码隐藏中被提取。我有一个站点。母版页,它在主体的星形和末端,因此Register.aspx元素都在表单中,对吗?您需要在该表单的母版页中放置asp contentplaceholder,并在子页面中使用相同的内容。。将此di放在该占位符中\Ex:我有一个名为Register.aspx的asp.net页面和代码隐藏文件Register.aspx.csi在您的aspx页面中,您的控件是否嵌套在具有runat=“server”属性的表单标记中?如果您不将控件嵌套在服务器端表单中,它们将不会在代码隐藏中被提取。我有一个站点。母版页,它在主体的星形和末端,因此Register.aspx元素都在表单中,对吗?您需要在该表单的母版页中放置asp contentplaceholder,并在子页面中使用相同的内容。。将这个di放在那个占位符中\n我确实写过,但我得到:对象引用没有设置为对象的实例。你现在能用你更改的内容更新你的C代码吗?你是在什么地方遇到对象引用问题的?我刚刚尝试了完全相同的方法,效果很好。我要做的唯一更改是将字符串变量从Password重命名为Passwd,如更新中所示。但这不是你得到异常的地方。我一定是遗漏了什么,因为我总是得到异常:\n你能不能给我发一些你的文件,因为我一定是遗漏了什么。啊!使用母版页时,需要将标记嵌套在
asp:Content
标记中。请检查我在回答中对Register.aspx的编辑。我确实写过,但我得到:对象引用未设置为对象的实例。您现在可以用更改的内容更新您的C代码吗?你是在什么地方遇到对象引用问题的?我刚刚尝试了完全相同的方法,效果很好。我要做的唯一更改是将字符串变量从Password重命名为Passwd,如更新中所示。但这不是你得到异常的地方。我一定是遗漏了什么,因为我总是得到异常:\n你能不能给我发一些你的文件,因为我一定是遗漏了什么。啊!使用母版页时,需要将标记嵌套在
asp:Content
标记中。请检查我对Register.aspx.sqlserver标记的编辑是否已删除,因为您的问题不包含任何与sql server相关的信息。@xRed:我认为您需要在默认页面中添加更多标记,以便其他人发现文本框为空的情况。@Kash我不知道如何初始化它,我想,我该怎么做?@xRed:你在更新中做到了:
string Username=
sqlserver标记已被删除,因为你的问题不包含任何与sql server相关的信息。@xRed:我认为你需要在默认页面中添加更多标记,以便其他人发现文本框为空的情况。@Kash我不知道怎么做初始化它我想,我该怎么做?@xRed:你用你的更新做到了:
string Username=