sjc0311 Posted February 28, 2003 Posted February 28, 2003 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 doesn't 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) Quote
*Gurus* Derek Stone Posted February 28, 2003 *Gurus* Posted February 28, 2003 Once you declare a WebControl as shown below, you can access it throughout the current page using its ID property. <asp:TextBox id="MainTextBox" runat="server" /> Sub Page_Load(sender As Object, e As EventArgs) MainTextBox.Text = "foobar" End Sub Quote Posting Guidelines
sjc0311 Posted February 28, 2003 Author Posted February 28, 2003 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 doesn't point to an instance of an object. Thanks for any additional help! Quote
*Gurus* Derek Stone Posted February 28, 2003 *Gurus* Posted February 28, 2003 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. Quote Posting Guidelines
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.