0

I am writing an web app in asp.net using .net 3.5 and vs 2010;

I creatd a web user control (.ascx), and i want to insert it to a panel taht i created in a simple cs class (not a aspx.cs),

my class is in App_Code, the problem is that the class dous not Identifies the user control name so for example i cant do:

public class MyClass
{
    public MyClass() { }

    public void foo(MyCtrl i_MyCtrl)

    {
       //doing somthing 
    }  
}

I search for an answer on the web, and people say it cant be done in .net 2 and vs 2005/8 but nothing about .net 3.5 and vs 2010

the thing is that you cant create a butten (a buttten is a control) , so this make me think maybe there a chance to create a dynamically user control in a simple cs file.

please helpe

thanks.

user723686
  • 265
  • 1
  • 15
  • 21

1 Answers1

0

The LoadControl method reads in a given ASCX and constructs a Control instance from it - you are then free to add it to the page's control tree. This has been a feature from the beginning in ASP.NET:

Control c = this.LoadControl("myAscx.ascx");
this.myPanel.Controls.Add( c );
Dai
  • 141,631
  • 28
  • 261
  • 374
  • this did not work i get an error: System.Web.HttpException: The file '/WebSite/WebUserControlTest.ascx' does not exist. for : Control c = this.LoadControl("~/WebSite/UserControls/WebUserControlTest.ascx"); – user723686 Mar 23 '12 at 08:26