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

后端 未结 6 1295
情话喂你
情话喂你 2020-12-09 06:41

I am trying to dynamically load a user control in an asp.web site. However due to how asp.net websites projects are setup (I think), I am not able to access reach the type d

相关标签:
6条回答
  • 2020-12-09 06:45

    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.

    0 讨论(0)
  • 2020-12-09 06:49

    The subject of this post is a bit misleading. If you just want to add a control dynamically, you will not have to reference the control and therefore you can just add it with something simple like:

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Controls.Add(Page.LoadControl("~/MyControl.ascx")); 
    }
    

    without any namespace hassel.

    0 讨论(0)
  • 2020-12-09 06:53

    the reference is not enough using

    <%@ Reference Control="~/Controls/WebControl1.ascx">
    

    in the aspx file is just one part of the answer.

    you need also to add the calssName in the User Control aspx file

    <%@ Control ClassName="WebControl1" Language="C#" AutoEventWireup="true" CodeFile="WebControl1.ascx.cs" Inherits="AnySpaceName.DateSelector" %>
    

    and then you can use the userontrol in your aspx file

    AnySpaceName.WebControl1 WC = (AnySpaceName.WebControl1)Page.LoadControl("~/WebControl1.ascx");    
    
    0 讨论(0)
  • 2020-12-09 06:56

    It can easily be done using namespaces. Here's an example:

    WebControl1.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebControl1.ascx.cs" Inherits="MyUserControls.WebControl1" %>
    

    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.

    0 讨论(0)
  • 2020-12-09 07:03

    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:

    <%@ Reference Control="~/Controls/WebControl1.ascx">
    

    Keep in mind it often takes a few minutes (or sometimes a build) for IntelliSense to pick this up.

    0 讨论(0)
  • 2020-12-09 07:10

    Namespaces are not supported under the website model. As such, I could not get any of the solutions proposed to work. However, there is a solution. Create an interface and place it into app code and then implement the interface in the user control. You can cast to the interface and it works.

    0 讨论(0)
提交回复
热议问题