0

I have a page with a with a form i when i fill the form i want to access that form data in my c# codebehind file.

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;
    }
}

What function i have to use to read from a asp.net textbox and pass it to by c# codebehind variable?

4
  • sqlserver tag is removed bcz your question does not contain any info related to sql server. Commented Aug 22, 2012 at 17:29
  • @xRed: I think you need to add more of your markup in your Default page for others to spot what might be the case of the textbox being null.. Commented Aug 22, 2012 at 17:37
  • @Kash i dont know how to initialize it i think, how can i do it? Commented Aug 22, 2012 at 18:26
  • @xRed: You did it with your update: string Username = Commented Aug 22, 2012 at 19:11

2 Answers 2

2

UPDATE:

You need to enclose your markup in <asp:Content></asp:Content> tags because you are using a master page.


If you have used Visual Studio to generate your web project, then you can see Register.aspx.designer.cs which should have code like below. If you don't then add it in the class (you can add it in Register.aspx.cs or Register.aspx.designer.cs).

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

Then you can use

string UserName = Utilizador.Text;

UPDATE:

I just tried this and seems to work fine:

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>
Sign up to request clarification or add additional context in comments.

5 Comments

I did has you wrote but i got: Object reference not set to an instance of an object.
Can you update your C# code now with what you changed? At what place are you getting the object reference issue?
I just tried the exact same thing and it works. The only change I had to do was rename the string variable from Password to Passwd as shown in my update. But that is not where you are getting the exception.
I must be missing something because i always get that Exception :\ Can u somehow send me your files because i must be missing something.
Ah! You need to nest your markup in asp:Content tags as you are using a master page. Please check my edit in answer for Register.aspx.
1

Make sure your code inside a <form runat="server"></form> tag?

Your code should look like:

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

EDIT

To reference controls in a master page/content placeholder

4 Comments

Ex: I have asp.net page named Register.aspx and the codebehind file Register.aspx.cs
In your ASPX page, are your controls nested inside a form tag with the attribute runat="server"? If you don't nest the controls inside a server-side form, they won't get picked up in the codebehind.
I have a Site.Master page that has <form runat="server"></form> at the star and end of the body so the Register.aspx elements are all inside the form right?
you need to put asp contentplaceholder in master page inside that form and use the the same in your child pages.. pUt this di inside that place holder\

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.