0

当我填写表单时,我有一个带有表单的页面,我想在我的 c# 代码隐藏文件中访问该表单数据。

注册.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"/>

注册.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# 代码隐藏变量将其传递给?

4

2 回答 2

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;

更新:

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

注册.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;
    }

注册.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>
于 2012-08-22T17:15:45.490 回答
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>

编辑

引用母版页/内容占位符中的控件

于 2012-08-22T15:51:13.757 回答