LoadControl by type

Discussion in 'ASP .Net Web Controls' started by ame, Mar 2, 2009.

  1. ame

    ame Guest

    Instead of loading a User Control by filename

    -----
    Control myCtrl= LoadControl( "~/WebUserControl1.ascx");
    this.PlaceHolder1.Controls.Add(myCtrl);
    ----
    - which works-


    how can the User Control be loaded by class name ?
    I tried

    ----
    Control myCtrl = LoadControl(typeof(WebUserControl1), new Object[]{});
    this.PlaceHolder1.Controls.Add(myCtrl);
    ----

    but this does not work. There is no runtime error, but the control is not
    shown.

    Is there some action missing ?
    ame, Mar 2, 2009
    #1
    1. Advertising

  2. > Instead of loading a User Control by filename
    >
    > -----
    > Control myCtrl= LoadControl( "~/WebUserControl1.ascx");
    > this.PlaceHolder1.Controls.Add(myCtrl);
    > ----
    > - which works-
    >
    >
    > how can the User Control be loaded by class name ?
    > I tried
    >
    > ----
    > Control myCtrl = LoadControl(typeof(WebUserControl1), new
    > Object[]{});
    > this.PlaceHolder1.Controls.Add(myCtrl);
    > ----
    >
    > but this does not work. There is no runtime error, but the control is
    > not
    > shown.
    >
    > Is there some action missing ?


    No, there is no action missing. Something else is missing, the path.
    How should the LoadControl method know where the .ascx file is located?
    And, you might have two or more UserControls in your website
    application, both or all having the same classname; how should the
    LoadControl method know which of them it has to load?


    Harald M. Genauck

    "VISUAL STUDIO one" - http://www.visualstudio1.de
    "ABOUT Visual Basic" - http://www.aboutvb.de
    Harald M. Genauck, Mar 3, 2009
    #2
    1. Advertising

  3. ame

    ame Guest

    given the following files:
    --------------------- WebUserControl1.ascx.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace testns
    {
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
    protected override void Render(HtmlTextWriter writer)
    {
    writer.RenderBeginTag("h3");
    writer.Write("TESTTEXT");
    writer.RenderEndTag();
    }
    }
    }
    --------------------- WebUserControl1.ascx (splittet in lines)
    <%@ Control
    Language="C#"
    ClassName="TestElem"
    AutoEventWireup="true"
    CodeBehind="WebUserControl1.ascx.cs"
    Inherits="testns.WebUserControl1"
    %>
    ---------------------------------------------


    Sorry, I can't understand for what the ascx-file is necessary.

    >> There is no HTML-Code in the ascx-file, because it is built manually in the cs-file
    >> The ascx-file contains information like

    Control,
    CodeBehind,
    Inherits=
    which all are implicit or explicit contained by or in cs-file and the
    statement
    using namespace testns;
    ...
    Control myCtrl = LoadControl(typeof(WebUserControl1), ...)


    The solution shall not work in GUI, but only program driven - also
    EVentHandling made manually, so:
    I don't need a ascx-file for me and simply wants to generate a WebControl by
    code.

    Isn't that possible with "LoadControl" ?
    ame, Mar 3, 2009
    #3
  4. ame

    ame Guest

    got it:
    myCtrl.InitializeAsUserControl(this.Page);
    ame, Mar 3, 2009
    #4
  5. > Sorry, I can't understand for what the ascx-file is necessary.

    So, why did you create it if you won't need or use it?

    >>> There is no HTML-Code in the ascx-file, because it is built
    >>> manually in the cs-file
    >>> The ascx-file contains information like

    > Control,
    > CodeBehind,
    > Inherits=
    > which all are implicit or explicit contained by or in cs-file and the
    > statement
    > using namespace testns;
    > ...
    > Control myCtrl = LoadControl(typeof(WebUserControl1), ...)
    >
    >
    > The solution shall not work in GUI, but only program driven - also
    > EVentHandling made manually, so:
    > I don't need a ascx-file for me and simply wants to generate a
    > WebControl by
    > code.
    >
    > Isn't that possible with "LoadControl" ?


    LoadControl's purpose is to load an UserControl including the HTML
    stuff created in the designer part and stored in the .ascx file. If
    don't need the designer part so then you don't need to create a user
    control or .ascx file.

    If you want to create your own control from scratch, why don't you
    inherit your control from System.Web.UI.WebControls.WebControl, or, at
    least, from System.Web.UI.Control?


    Harald M. Genauck

    "VISUAL STUDIO one" - http://www.visualstudio1.de
    "ABOUT Visual Basic" - http://www.aboutvb.de
    Harald M. Genauck, Mar 3, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kurt

    Need help with LoadControl

    Kurt, Jul 1, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    429
    Natty Gur
    Jul 3, 2003
  2. huobazi
    Replies:
    1
    Views:
    667
    David Waz...
    Jul 3, 2003
  3. George Durzi

    LoadControl VirtualPath question

    George Durzi, Jul 31, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    466
    George Durzi
    Jul 31, 2003
  4. Keith Patrick
    Replies:
    2
    Views:
    3,619
    Keith Patrick
    Nov 16, 2005
  5. DC
    Replies:
    0
    Views:
    693
Loading...

Share This Page