Can't Access to TextBox threw WebControl

Discussion in 'ASP .Net Web Controls' started by Nimrod Rotner, Jun 5, 2007.

  1. Hi All,
    I can't understand what is the problem. I am using web control wich has a text box ob it and a public property that set and get that TextBox text value.

    The compilation pass but when i try to get or set that property, i get: "Object Reference no set to an instance of an object."

    Here is my Code:
    ----------------
    public class WebUserControl1 : System.Web.UI.UserControl
    {
    private string num;
    protected System.Web.UI.WebControls.TextBox txtName;
    public string txt
    {
    get
    {
    return txtName.Text;
    }
    set
    {
    txtName.Text=value;
    }
    }

    private void Button1_Click(object sender, System.EventArgs e)
    {

    WebUserControl1 ctl=new WebUserControl1();

    Response.Write(ctl.txt);
     
    Nimrod Rotner, Jun 5, 2007
    #1
    1. Advertising

  2. Nimrod Rotner

    PlatinumBay Guest

    Nimrod,

    While your code may compile, you are creating a new WebUserControl1 object.
    Unless the constructor of that object instantiates the txtName object, you
    will get the Object Reference exception trying to access the Text property
    of the txtName object.

    Hope this helps,


    Steve

    <Nimrod Rotner> wrote in message
    news:OV$...
    > Hi All,
    > I can't understand what is the problem. I am using web control wich has a
    > text box ob it and a public property that set and get that TextBox text
    > value.
    >
    > The compilation pass but when i try to get or set that property, i get:
    > "Object Reference no set to an instance of an object."
    >
    > Here is my Code:
    > ----------------
    > public class WebUserControl1 : System.Web.UI.UserControl
    > {
    > private string num;
    > protected System.Web.UI.WebControls.TextBox txtName;
    > public string txt
    > {
    > get
    > {
    > return txtName.Text;
    > }
    > set
    > {
    > txtName.Text=value;
    > }
    > }
    >
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    >
    > WebUserControl1 ctl=new WebUserControl1();
    >
    > Response.Write(ctl.txt);
    >
     
    PlatinumBay, Jun 5, 2007
    #2
    1. Advertising

  3. Hi,
    Thanks for your fast reply. Can you tell me how should i write that init
    code for the text box in the constructor ?


    Thanks


    "PlatinumBay" wrote:

    > Nimrod,
    >
    > While your code may compile, you are creating a new WebUserControl1 object.
    > Unless the constructor of that object instantiates the txtName object, you
    > will get the Object Reference exception trying to access the Text property
    > of the txtName object.
    >
    > Hope this helps,
    >
    >
    > Steve
    >
    > <Nimrod Rotner> wrote in message
    > news:OV$...
    > > Hi All,
    > > I can't understand what is the problem. I am using web control wich has a
    > > text box ob it and a public property that set and get that TextBox text
    > > value.
    > >
    > > The compilation pass but when i try to get or set that property, i get:
    > > "Object Reference no set to an instance of an object."
    > >
    > > Here is my Code:
    > > ----------------
    > > public class WebUserControl1 : System.Web.UI.UserControl
    > > {
    > > private string num;
    > > protected System.Web.UI.WebControls.TextBox txtName;
    > > public string txt
    > > {
    > > get
    > > {
    > > return txtName.Text;
    > > }
    > > set
    > > {
    > > txtName.Text=value;
    > > }
    > > }
    > >
    > > private void Button1_Click(object sender, System.EventArgs e)
    > > {
    > >
    > > WebUserControl1 ctl=new WebUserControl1();
    > >
    > > Response.Write(ctl.txt);
    > >

    >
    >
     
    Nimrod Rotner, Jun 6, 2007
    #3
  4. Nimrod Rotner

    marss Guest

    Nimrod Rotner wrote:

    > WebUserControl1 ctl=new WebUserControl1();


    Hi,
    I guess txtName is the TextBox control that is defined elsewhere in
    the WebUserControl1.ascx file.
    It is not initialized because you use incorrect method to create
    instance of the user control.
    Creation by means of "new" operator is suitable for a web control but
    not for a user control.
    Use next approach:

    WebUserControl1 ctl = (WebUserControl1)LoadControl("~/.../
    WebUserControl1.ascx");

    Regards, Mykola
    http://marss.co.ua
     
    marss, Jun 6, 2007
    #4
  5. Nimrod Rotner

    PlatinumBay Guest

    Nimrod,

    Your initialization in the constructor would look like:

    public WebUserControl1
    {
    this.txtName = new System.Web.UI.WebControls.TextBox();
    // possibly set a default value
    this.txtName.Text = "";
    }

    Hope this helps,


    Steve

    "Nimrod Rotner" <Nimrod > wrote in message
    news:...
    > Hi,
    > Thanks for your fast reply. Can you tell me how should i write that init
    > code for the text box in the constructor ?
    >
    >
    > Thanks
    >
    >
    > "PlatinumBay" wrote:
    >
    >> Nimrod,
    >>
    >> While your code may compile, you are creating a new WebUserControl1
    >> object.
    >> Unless the constructor of that object instantiates the txtName object,
    >> you
    >> will get the Object Reference exception trying to access the Text
    >> property
    >> of the txtName object.
    >>
    >> Hope this helps,
    >>
    >>
    >> Steve
    >>
    >> <Nimrod Rotner> wrote in message
    >> news:OV$...
    >> > Hi All,
    >> > I can't understand what is the problem. I am using web control wich has
    >> > a
    >> > text box ob it and a public property that set and get that TextBox text
    >> > value.
    >> >
    >> > The compilation pass but when i try to get or set that property, i get:
    >> > "Object Reference no set to an instance of an object."
    >> >
    >> > Here is my Code:
    >> > ----------------
    >> > public class WebUserControl1 : System.Web.UI.UserControl
    >> > {
    >> > private string num;
    >> > protected System.Web.UI.WebControls.TextBox txtName;
    >> > public string txt
    >> > {
    >> > get
    >> > {
    >> > return txtName.Text;
    >> > }
    >> > set
    >> > {
    >> > txtName.Text=value;
    >> > }
    >> > }
    >> >
    >> > private void Button1_Click(object sender, System.EventArgs e)
    >> > {
    >> >
    >> > WebUserControl1 ctl=new WebUserControl1();
    >> >
    >> > Response.Write(ctl.txt);
    >> >

    >>
    >>
     
    PlatinumBay, Jun 6, 2007
    #5
  6. Nimrod Rotner

    PlatinumBay Guest

    Nimrod,

    You can also initialize it when it is declared, for example:

    protected System.Web.UI.WebControls.TextBox txtName = new
    System.Web.UI.WebControls.TextBox();

    Hope this helps,


    Steve

    "Nimrod Rotner" <Nimrod > wrote in message
    news:...
    > Hi,
    > Thanks for your fast reply. Can you tell me how should i write that init
    > code for the text box in the constructor ?
    >
    >
    > Thanks
    >
    >
    > "PlatinumBay" wrote:
    >
    >> Nimrod,
    >>
    >> While your code may compile, you are creating a new WebUserControl1
    >> object.
    >> Unless the constructor of that object instantiates the txtName object,
    >> you
    >> will get the Object Reference exception trying to access the Text
    >> property
    >> of the txtName object.
    >>
    >> Hope this helps,
    >>
    >>
    >> Steve
    >>
    >> <Nimrod Rotner> wrote in message
    >> news:OV$...
    >> > Hi All,
    >> > I can't understand what is the problem. I am using web control wich has
    >> > a
    >> > text box ob it and a public property that set and get that TextBox text
    >> > value.
    >> >
    >> > The compilation pass but when i try to get or set that property, i get:
    >> > "Object Reference no set to an instance of an object."
    >> >
    >> > Here is my Code:
    >> > ----------------
    >> > public class WebUserControl1 : System.Web.UI.UserControl
    >> > {
    >> > private string num;
    >> > protected System.Web.UI.WebControls.TextBox txtName;
    >> > public string txt
    >> > {
    >> > get
    >> > {
    >> > return txtName.Text;
    >> > }
    >> > set
    >> > {
    >> > txtName.Text=value;
    >> > }
    >> > }
    >> >
    >> > private void Button1_Click(object sender, System.EventArgs e)
    >> > {
    >> >
    >> > WebUserControl1 ctl=new WebUserControl1();
    >> >
    >> > Response.Write(ctl.txt);
    >> >

    >>
    >>
     
    PlatinumBay, Jun 6, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary

    What Page threw the exception

    Gary, Jan 23, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    381
    Steven Cheng[MSFT]
    Jan 28, 2004
  2. ruca

    "The server threw an exception"

    ruca, Jun 1, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    728
  3. Dave

    LOOP THREW A REPEATER CONTROL

    Dave, Jul 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,644
    Karl Seguin
    Jul 29, 2005
  4. Loop threw datalist

    , Jan 23, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    738
    =?Utf-8?B?RWx0b24gVw==?=
    Jan 23, 2006
  5. Luis Ramírez.

    Can a webcontrol (webcontrol.dll) have a configuration file?

    Luis Ramírez., Jul 5, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    217
    Luis Ramírez
    Jul 6, 2004
Loading...

Share This Page