2012-08-22 66 views
0

當我填充表單時,我想要在我的c#代碼隱藏文件中訪問該表單數據,我有一個帶有表單的頁面。從asp.net讀取文本框的值,並將其傳遞給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變量傳遞給它?

+0

sqlserver的標記將被刪除BCZ你的問題中不包含與SQL Server相關的任何信息。 –

+0

@xRed:我認爲您需要在您的默認頁面中添加更多標記以供其他人發現文本框爲空的情況。 – Patrick

+0

@Kash我不知道如何初始化它我想,我該怎麼做呢? – xRed

回答

2

UPDATE:

你需要,因爲你正在使用附上您的標記在<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; 

UPDATE:

我只是嘗試這樣做,似乎很好地工作:

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> 
+0

我曾經寫過,但是我得到了:_Object reference not set to an instance of一個object._ – xRed

+0

你現在可以更新你的C#代碼嗎?你在什麼地方得到對象引用問題? – Kash

+0

對實際代碼做了一些更新。 – xRed

1

確保您的代碼在​​標記內?

您的代碼應該是這樣的:

<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

+0

例如:我有一個名爲Register.aspx的asp.net頁面和代碼隱藏文件Register.aspx.cs – xRed

+0

在你的ASPX頁面中,你的控件嵌套在形式標籤的屬性** runat =「server」**?如果您不將這些控件嵌套在服務器端表單中,它們將不會在代碼隱藏中獲得。 – Tim

+0

我有一個Site.Master頁面的

位於主體的尾部,所以Register.aspx元素都在表單右側? – xRed

相關問題