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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Page.Control

Status
Not open for further replies.
Apr 9, 2007
16
US
I have a code

Page.Control.add(label);

Now that works in default.aspx, but when I try to use it in my own class(MyClass.cs). It doesnt work.

Whats the proper way to write that in a class?
 
So you want to add the control, "label", to your custom class? Does your class inherit/extend System.Web.UI.Page? If so, then I can't see why that doesn't work.

I guess I just need to ask what you are trying to do. The way I'm imagining it is:

Code:
namespace MyApp
{
   using System.Web.UI;

   public class MyClass
   {
      private ControlCollection _controls;
      public ControlCollection Controls
      {
         get { return _controls; }
         set { _controls = value; }
      }
   }
}

Ron Wheeler
Tekdev Open Source Development
 
You can try this, it is in VB but I'm sure you can convert it easily:

'This is in the class file:
Code:
Function AddControlToPage(ByVal myPage As System.Web.UI.Page)
   Dim myLabel As New Label
   myLabel.ID = "MyDynamicLabel"
   myLabel.Text = "This is a label control added to the page from a class"
   myPage.FindControl("Form1").Controls.Add(myLabel)
End Function
'This goes in the Page_Init of the page:
Code:
theClass.AddControlToPage(Me)
 
I'm not really VB.NET programmer, but wouldn't you pass the parameter ByRef so that it's updating the original page, not just a copy? Which doesn't really matter, since he is using C#. Which would then need "ref".

So, in C#:
Code:
[blue]public void[/blue] AddControlToPage([blue]ref[/blue] System.Web.UI.Page myPage)
{
   Label myLabel = [blue]new[/blue] new Label();
   myLabel.ID = [COLOR=#CC0000]"MyDynamicLabel"[/color];
   myLabel.Text = [COLOR=#CC0000]"This is a label control added to the page from a class"[/color];
   myPage.Controls.Add(myLabel);
}

Ron Wheeler
Tekdev Open Source Development
 
Alrighty. Frist of all Id like to thank you all for your efforts in helping me tackling this down. Like they say, two heads are better than one. Saved me an extra day of sleep:)

Now back to the problem.
I tried all of you guys suggestions, and from your hints, Ive manage to try something, and it worked alright, but not sure if its the correct way of doing it. Ah well, as lonog as it works, Im ok with it. I still have alot of stuff to do.

this is what I did. I'll modify you guys example.

public void AddControlToPage()
{
Label myLabel = new Label();
myLabel.ID = "MyDynamicLabel";
myLabel.Text = "This is a label control added to the page from a class";

Page myPage = HttpContext.Current.Handler as Page;
myPage.Controls.Add(myLabel);
}

And thats it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top