Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using web server controls 1

Status
Not open for further replies.

mattyjim

Programmer
Mar 11, 2007
5
GB
hello!

i've created an instance of a class within an .aspx.

the class that i've instantiated has a method that contains the following code:

TextBox myTextBox = new TextBox();
myTextBox.Text = "Sample Text";


whenever i call this method i receive no errors, but i don't achieve the desired result either (no web control appears).

(note: i can print text from this method, so i'm fairly sure that there's nothing wrong with the way i'm using the class)

i'm basically trying to create a class that i can call from a web form in order to programmatically create a web server control (textbox in this case) inside the same web form.

the basic rationale behind this is that i will eventually be able to build the contents of web forms dynamically, rather than having to hard-code page designs.

i'm new to asp.net, and so i'm assuming that i'm misunderstanding something fairly fundamental here.

can anyone help me out?
 
You've created a new TextBox but you haven't added it to the page, so whilst it exists you'll never see it. To add it to the page, you could create a place holder at desing time and then add it to that control at run time e.g.
Code:
PlaceHolder1.Controls.Add(myTextBox)


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
thanks for your reply, ca8msm!

i think i see what you mean.

the problem now is that i can't seem to access PlaceHolder1 from inside my class.

is there a way to pass in a reference to PlaceHolder1 or something?
 
You probably shouldn't pass in a reference to the placeholder (although you could pass it ByRef if you wanted). Instead, I'd make a function in your class that returned a TextBox and have your page call that function and add it that way.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
thanks ca8msm!

i'll give that a try.



thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top