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

时间:2012-08-22 15:48:40

标签: c# asp.net visual-studio-2012

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

Register.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"/>

Register.aspx.cs:

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.designer.cs:

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变量传递给它?

2 个答案:

答案 0 :(得分:2)

<强>更新

您需要将标记括在<asp:Content></asp:Content>代码中,因为您使用的是母版页。


如果您已使用Visual Studio生成Web项目,那么您可以看到Register.aspx.designer.cs,其中应包含如下代码。如果不这样做,则将其添加到类中(可以将其添加到Register.aspx.cs或Register.aspx.designer.cs中)。

protected global::System.Web.UI.WebControls.TextBox Utilizador;

然后你可以使用

string UserName = Utilizador.Text;

更新:

我刚试过这个并且似乎工作正常:

<强> Register.aspx.cs

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

<强> Register.aspx.designer.cs

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>

答案 1 :(得分:1)

确保您的代码位于<form runat="server"></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>

修改

To reference controls in a master page/content placeholder