create dynamically web user control from a simple cs file (not aspx.cs ) in asp.net

581 views Asked by At

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.

1

There are 1 answers

1
Dai On

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 );