Accessing form objects from Codebehind

  • Thread starter Thread starter vbMark
  • Start date Start date
V

vbMark

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.
 
Hallo vbMark
vbMark said:
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
 
Hallo vbMark
vbMark said:
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
 
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!"
}
}
}
 
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.
 
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
 
Back
Top