Dynamically Load a user control (ascx) in a asp.net website?

Assuming the control exists in the same assembly as your web project, you need to add a reference directive in your . Aspx file.

Assuming the control exists in the same assembly as your web project, you need to add a reference directive in your . Aspx file, e. G: Keep in mind it often takes a few minutes (or sometimes a build) for IntelliSense to pick this up.

Thanks very much. I had assumed the reference was not needed if I pointed to the location of the code file. It seemed a little redundant to add the file but again I made an assumption... – Tuka Aug 17 '09 at 22:02 If you're dealing with a lot of usercontrols I really wouldn't recommend this approach.In my experience the solution to add the usercontrols to a namespace and then load them dynamically is a lot more versatile.

– Tchami Aug 17 '09 at 22:09 Yeah, it's a bit redundant but basically the reference directive is for ASP. NET so it can actually resolve the cast on Page. LoadControl at the appropriate time.

Glad I could help! – jscharf Aug 17 '09 at 22:11.

It can easily be done using namespaces. Here's an example: WebControl1. Ascx: Notice that Inherits references the namespace (MyUserControls), and not just the class name (WebControl1) WebControl1.ascx.Cs: namespace MyUserControls { public partial class WebControl1 : System.Web.UI.

UserControl { protected void Page_Load(object sender, EventArgs e) { } } } Notice that the class have been included in the namespace MyUserControls Default.aspx. Cs: using MyUserControls; public partial class _Default : System.Web.UI. Page { protected void Page_Load(object sender, EventArgs e) { var control = (WebControl1) Page.

LoadControl("~/WebControl1. Ascx"); } } This approach potentially allow you to redistribute your user controls (or keep them in a separate project) without the hassle of referencing them in your . Aspx files.

The reference is not enough using in the aspx file is just one part of the answer. You need also to add the calssName in the User Control aspx file and then you can use the userontrol in your aspx file AnySpaceName. WebControl1 WC = (AnySpaceName.

WebControl1) Page. LoadControl("~/WebControl1. Ascx").

Casting the user control this way may create many problems . My approach is to create a class (say control Class) put all the properties and method you need for casting in it and inherit this class from System.Web.UI. UserControl .

Then in your user cotrol code file instead of System.Web.UI. UserControl user this control class . Now when ever you need casting, cast with this class only .

It will be light casting as well.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions