Global Variables

sjc0311

New member
Joined
Feb 26, 2003
Messages
3
I want to declare some WebControls on at a global level so I will have access to them and then instantiate them in certain methods. I was just wondering how to do this. When I try to declare them in Page_Init() it doesnt work. Obviously, I"m very new to ASP.NET, so could anyone please explain how to declare globabl variables for a single page? (not in the global.aspx file)
 
Once you declare a WebControl as shown below, you can access it throughout the current page using its ID property.

Code:
<asp:TextBox id="MainTextBox" runat="server" />
Code:
Sub Page_Load(sender As Object, e As EventArgs)
    MainTextBox.Text = "foobar"
End Sub
 
I know my previous post was unclear, but here is what I actually wanted to do:

MyPlaceHolder.Controls.Add(GenerateTextBox());

then I would have something like this:

TextBox GenerateTextBox
{
TextBox MyTextBox = new TextBox();

return MyTextBox;
}



my question is how do I then get and set properties of this text box? I thought that I could declare it at the top of the scope like this:

TextBox MyTextBox;

Then I could instantiate it in the GenerateTextBox function, but when I try to maipulate properties it tells me that it doesnt point to an instance of an object. Thanks for any additional help!
 
C#:
TextBox MyTextBox = new TextBox();

void page_load(Object sender, EventArgs e) {
    MyTextBox.Text = "foobar";
    MyPlaceHolder.Controls.Add(MyTextBox);
}
That should work fine, even if you decide to modify properties after the control is added to its placeholder.
 
Back
Top