Does anyone know how to programtically add a control (from a class) to a web form? I have created a class, and I want it to add controls to whatever form calls it at runtime. For example. Public Class Sample Dim Label As New Label() Dim Panel1 As New Panel()
Conrols.Add(Panel1) Panel1.Controls.Add(Label) End Class Now I have a reference to that class file in a new project. I create a web form and call the object. Imports Sample Dim X as New Sample() If I do something like that I never see the control on the form.
Any ideas?
Hi! What you can do is to add a method to your class that will accept Page object as a parameter and will add to this page new controls. The method in following example is shared (static), so no need to construct an object Imports System.Web.UI Public Class
Sample Public Shared Sub AddControls(pg As Page) Dim lbl As New Label() lbl.id = "lblHello" lbl.Text = "Hello" pg.Control.Add(lbl) End Sub End Class And then in the Page_Load of your page you may call the method passing the page object to it: Sub Page_Load(src
As Object, e As EventArgs) ... Sample.AddControls(Me) End Sub Good luck...
Hello, Thanks to both of you for your solutions. I like this one because Anyone could use the class file that I create without having to put anything in the Page_load event of thier page. However I could use that latter solution in many other ways (thanks Jekam).
rbucktion - I cannot seem to get a .page.whatever. Once I type in System.Web.HttpContext.Current. I get many other options, but none of them are page. My class is inheriting from System.Web.UI.Page Do I need to add an import statement, or should my class be
inheriting from a different class? Much thanks.
Hello jekaM, Thanks for you solution. I seem to get something wierd happening when I try to add a textbox as opposed to a label. Dim lbl As New Label() lbl.id = "lblHello" lbl.Text = "Hello" pg.Controls.Add(lbl) Dim _txtbox As New Textbox() _txtbox.Text = "test"
pg.Controls.Add(_txtbox) With the above code I get an error on the textbox saying: Control '_ctl0' of type 'TextBox' must be placed inside a form tag with runat=server. Do you know why I get this error when creating the textbox but not the label? Thank you.
My above example was incorrect, and I appologize. Your class inherits from System.Web.UI.Page? How exactly are you planning to use this class? If you are developing a class to sit on top of the Page Class to extend functionality you merely have to access the
Controls collection of your class (it is inherited from Page). [C#]
Yes, oddly enought, this only seems to work for labels. Im sure there is something I am missing. All of the following will give me errors (until I rem out all but the labels) Dim lbl As New Label() lbl.id = "lblHello" lbl.Text = "Hello" pg.Controls.Add(lbl)
Dim lbl2 As New Label() lbl2.Text = "Another Label" pg.Controls.Add(lbl2) 'Dim _lnk As New LinkButton() '_lnk.ID = "lnkID" '_lnk.Text = "Parent1" 'pg.Controls.Add(_lnk) 'Dim _txtbox As New Textbox() '_txtbox.Text = "test" 'pg.Controls.Add(_txtbox)
runtime errors. Control '_ctl1' of type 'TextBox' must be placed inside a form tag with runat=server. I am only getting the above errors with anything other than labels. If I use the label sample provided, it works perfectly. Here is the code inside of my class.
Public sub Construct_(pg As Page) Dim lbl As New Label() lbl.id = "lblHello" lbl.Text = "Hello" pg.Controls.Add(lbl) Dim lbl2 As New Label() lbl2.Text = "Another Label" pg.Controls.Add(lbl2) Dim _txtbox As New Textbox() _txtbox.Text = "test" pg.Controls.Add(_txtbox)
End Sub If I rem out the textbox one, it works just fine.
Well, eventually I was wanting to make it a server control that could accept either single defaults or an array of data. Ultimately I am building an asp.net treeview control that works in both netscape and IE with more that what is available with microsofts
asp.net treeview control (that works only in IE) I would like to use the object as such: for single values: Dim newMenu As NewMenu() newMenu.Name = "WinNT" newMenu.Parent1 = "System32" newMenu.Parent1.Child1 = "Spool" newMenu.Parent1.Child1.URL = "www.yahoo.com"
newMenu.Parent2 = "Temp" newMenu.Construct_ Just for an example(above) The following code above would build the object with these attributes: WinNT would be the ID and the title of the group - it would also be bold by default Spool would be clickable WinNT
System32 Spool Temp The above would be collapsable/expandable. (that part is easy)
aikeith
Participant
1375 Points
276 Posts
Add controls to current form from a class
Nov 01, 2002 02:16 PM|LINK
JekaM
Member
185 Points
37 Posts
Re: Add controls to current form from a class
Nov 01, 2002 08:27 PM|LINK
rbuckton
Member
547 Points
124 Posts
Re: Add controls to current form from a class
Nov 02, 2002 06:37 PM|LINK
Senior Consultant
Microsoft
aikeith
Participant
1375 Points
276 Posts
Re: Add controls to current form from a class
Nov 04, 2002 11:34 AM|LINK
aikeith
Participant
1375 Points
276 Posts
Re: Add controls to current form from a class
Nov 04, 2002 12:10 PM|LINK
rbuckton
Member
547 Points
124 Posts
Re: Add controls to current form from a class
Nov 04, 2002 12:10 PM|LINK
... protected override void OnLoad(EventArgs e) { this.Controls.Add(myControl); } ...[/C#] [VB][/VB]Senior Consultant
Microsoft
aikeith
Participant
1375 Points
276 Posts
Re: Add controls to current form from a class
Nov 04, 2002 12:25 PM|LINK
rbuckton
Member
547 Points
124 Posts
Re: Add controls to current form from a class
Nov 04, 2002 02:36 PM|LINK
Senior Consultant
Microsoft
aikeith
Participant
1375 Points
276 Posts
Re: Add controls to current form from a class
Nov 04, 2002 03:17 PM|LINK
aikeith
Participant
1375 Points
276 Posts
Re: Add controls to current form from a class
Nov 04, 2002 03:24 PM|LINK