Accessing form objects from Codebehind

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

Benjamin Bittner

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
 
B

Benjamin Bittner

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
 
M

mortb

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!"
}
}
}
 
V

vbMark

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

mortb

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top