Add controls to current form from a classhttp://forums.asp.net/t/78976.aspx/1?Add+controls+to+current+form+from+a+classWed, 11 Aug 2004 05:26:41 -04007897678976http://forums.asp.net/p/78976/78976.aspx/1?Add+controls+to+current+form+from+a+classAdd controls to current form from a class 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? 2002-11-01T14:16:36-05:0079250http://forums.asp.net/p/78976/79250.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class 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 = &quot;lblHello&quot; lbl.Text = &quot;Hello&quot; 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... 2002-11-01T20:27:38-05:0079500http://forums.asp.net/p/78976/79500.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class You could also access the currently executing page from the current HttpContext: <pre class="prettyprint">System.Web.HttpContext.Current.Page.Controls.Add(<i>myControl</i>);</pre> 2002-11-02T18:37:24-05:0080014http://forums.asp.net/p/78976/80014.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class 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. 2002-11-04T11:34:27-05:0080039http://forums.asp.net/p/78976/80039.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class 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 = &quot;lblHello&quot; lbl.Text = &quot;Hello&quot; pg.Controls.Add(lbl) Dim _txtbox As New Textbox() _txtbox.Text = &quot;test&quot; 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. 2002-11-04T12:10:12-05:0080040http://forums.asp.net/p/78976/80040.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class 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#]<pre class="prettyprint">... protected override void OnLoad(EventArgs e) { this.Controls.Add(<i>myControl</i>); } ...</pre>[/C#] [VB]<pre class="prettyprint">... Protected Overrides Sub OnLoad(ByVal e As EventArgs) Me.Controls.Add(<i>myControl</i>) End Sub ...</pre>[/VB] 2002-11-04T12:10:46-05:0080049http://forums.asp.net/p/78976/80049.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class 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 = &quot;lblHello&quot; lbl.Text = &quot;Hello&quot; pg.Controls.Add(lbl) Dim lbl2 As New Label() lbl2.Text = &quot;Another Label&quot; pg.Controls.Add(lbl2) 'Dim _lnk As New LinkButton() '_lnk.ID = &quot;lnkID&quot; '_lnk.Text = &quot;Parent1&quot; 'pg.Controls.Add(_lnk) 'Dim _txtbox As New Textbox() '_txtbox.Text = &quot;test&quot; 'pg.Controls.Add(_txtbox) 2002-11-04T12:25:40-05:0080161http://forums.asp.net/p/78976/80161.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class Build errors or runtime errors? could you post the Trace? 2002-11-04T14:36:16-05:0080191http://forums.asp.net/p/78976/80191.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class 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 = &quot;lblHello&quot; lbl.Text = &quot;Hello&quot; pg.Controls.Add(lbl) Dim lbl2 As New Label() lbl2.Text = &quot;Another Label&quot; pg.Controls.Add(lbl2) Dim _txtbox As New Textbox() _txtbox.Text = &quot;test&quot; pg.Controls.Add(_txtbox) End Sub If I rem out the textbox one, it works just fine. 2002-11-04T15:17:11-05:0080195http://forums.asp.net/p/78976/80195.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class 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 = &quot;WinNT&quot; newMenu.Parent1 = &quot;System32&quot; newMenu.Parent1.Child1 = &quot;Spool&quot; newMenu.Parent1.Child1.URL = &quot;www.yahoo.com&quot; newMenu.Parent2 = &quot;Temp&quot; 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) 2002-11-04T15:24:00-05:0081715http://forums.asp.net/p/78976/81715.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class JekaM, Thanks for your help before. Do you know how I can also pass the (pg As Page) to an Event Handler? Thanks again 2002-11-06T16:47:15-05:0082753http://forums.asp.net/p/78976/82753.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class The right way to do this is create a PlaceHolder and create the control on the PlaceHolder. This PlaceHolder must be inside a FORM tag and the form must run on server 2002-11-08T01:00:01-05:00662073http://forums.asp.net/p/78976/662073.aspx/1?Re+Add+controls+to+current+form+from+a+classRe: Add controls to current form from a class Hi! I have a web application that displays a TreeView on one of the pages. It works fine with IE. But it doesn't work on NetScape. Kindly suggest some remedy for the same. An alternative that works both woth IE and NetScape is more preferable. Thanks in Advance Regards Harmik 2004-08-11T05:08:53-04:00