Accessing form objects from Codebehind

Discussion in 'ASP .Net' started by vbMark, Jun 29, 2004.

  1. vbMark

    vbMark Guest

    Hi there,

    Using ASP.NET and C# I am trying to access an object on the main form from
    the "Codebehind" file default.aspx.cs but can't figure out how.

    If I have Textbox1 then how do I put text in it from default.aspx.cs?

    Thanks.

    --
    http://www.vbmark.com/
    A good place to start.
    vbMark, Jun 29, 2004
    #1
    1. Advertising

  2. Hallo vbMark
    "vbMark" <> schrieb im Newsbeitrag
    news:Xns95175B0BF1A16noemailcom@130.133.1.4...
    > Hi there,
    >
    > Using ASP.NET and C# I am trying to access an object on the main form from
    > the "Codebehind" file default.aspx.cs but can't figure out how.
    >
    > If I have Textbox1 then how do I put text in it from default.aspx.cs?
    >

    textbox1.Text = "New Text here",
    but before you can do that you have to tell the codebehind module, that
    there is a textbox called "textbox1" on the aspx page. im sorry, but i am
    not a c# expert, (or c# newbie... :p), but in vb, you do it this way:
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    after this you can access every textbox property.

    regards benni
    Benjamin Bittner, Jun 29, 2004
    #2
    1. Advertising

  3. Hallo vbMark
    "vbMark" <> schrieb im Newsbeitrag
    news:Xns95175B0BF1A16noemailcom@130.133.1.4...
    > Hi there,
    >
    > Using ASP.NET and C# I am trying to access an object on the main form from
    > the "Codebehind" file default.aspx.cs but can't figure out how.
    >
    > If I have Textbox1 then how do I put text in it from default.aspx.cs?
    >
    > Thanks.
    >
    > --

    The c# declaration is:

    protected System.Web.UI.WebControls.TextBox TextBox1;

    regards benni
    Benjamin Bittner, Jun 29, 2004
    #3
  4. vbMark

    mortb Guest

    Something like:

    file "page.aspx:":

    <asp:textbox id="myTxt" runat="server"/>

    -----------------------------------------------------
    codebehind file "page.aspx.cs":

    namespace XX
    {
    public class myPage
    {
    protected TextBox myTxt;

    private void Page_Load(object sender, System.EventArgs e)
    {
    myTxt.Text = "Hello world!"
    }
    }
    }
    ------------------------------------------------
    cheers,
    mortb

    "vbMark" <> wrote in message
    news:Xns95175B0BF1A16noemailcom@130.133.1.4...
    > Hi there,
    >
    > Using ASP.NET and C# I am trying to access an object on the main form from
    > the "Codebehind" file default.aspx.cs but can't figure out how.
    >
    > If I have Textbox1 then how do I put text in it from default.aspx.cs?
    >
    > Thanks.
    >
    > --
    > http://www.vbmark.com/
    > A good place to start.
    mortb, Jun 29, 2004
    #4
  5. vbMark

    vbMark Guest

    I had the code in a function like this:

    public static void DoThing ( )
    {
    myTxt.Text = "Hello world!"
    }

    And it did not work but does work in Page_Load. Why is that?

    Thanks.


    "mortb" <> wrote in
    news::

    > Something like:
    >
    > file "page.aspx:":
    >
    > <asp:textbox id="myTxt" runat="server"/>
    >
    > -----------------------------------------------------
    > codebehind file "page.aspx.cs":
    >
    > namespace XX
    > {
    > public class myPage
    > {
    > protected TextBox myTxt;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > myTxt.Text = "Hello world!"
    > }
    > }
    > }
    > ------------------------------------------------
    > cheers,
    > mortb
    >
    > "vbMark" <> wrote in message
    > news:Xns95175B0BF1A16noemailcom@130.133.1.4...
    >> Hi there,
    >>
    >> Using ASP.NET and C# I am trying to access an object on the main form
    >> from the "Codebehind" file default.aspx.cs but can't figure out how.
    >>
    >> If I have Textbox1 then how do I put text in it from default.aspx.cs?
    >>
    >> Thanks.
    vbMark, Jun 29, 2004
    #5
  6. vbMark

    mortb Guest

    Is the function DoThing called somewhere? From where?
    If you add a call to DoThing in Page_Load I'm sure it will work

    cheers,
    mortb


    "vbMark" <> wrote in message
    news:Xns95176407D5268noemailcom@130.133.1.4...
    > I had the code in a function like this:
    >
    > public static void DoThing ( )
    > {
    > myTxt.Text = "Hello world!"
    > }
    >
    > And it did not work but does work in Page_Load. Why is that?
    >
    > Thanks.
    >
    mortb, Jun 30, 2004
    #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. Vinod Kumar
    Replies:
    5
    Views:
    11,919
    Vinod Kumar
    Dec 29, 2003
  2. Mat
    Replies:
    4
    Views:
    362
  3. Replies:
    3
    Views:
    1,326
  4. jdn
    Replies:
    3
    Views:
    189
  5. Derek Basch
    Replies:
    4
    Views:
    179
    -berlin.de
    Aug 16, 2006
Loading...

Share This Page