![](/img/trans.png)
[英]Pass value from a Repeater row to a textbox on button click in asp.net C#
[英]Read textbox value from asp.net and pass it to c# variable
當我填寫表單時,我要使用一個帶有表單i的頁面,我想在我的C#代碼隱藏文件中訪問該表單數據。
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變量?
更新:
您需要將標記括在<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>
確保您的代碼位於<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>
編輯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.