Home All Groups Group Topic Archive Search About
Author
2 Mar 2009 11:59 AM
ame
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 ?

Author
3 Mar 2009 12:33 AM
Harald M. Genauck
Show quote Hide quote
> 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
Author
3 Mar 2009 8:53 AM
ame
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" ?
Author
3 Mar 2009 9:39 PM
Harald M. Genauck
> 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?

Show quoteHide quote
>>> 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
Author
3 Mar 2009 1:56 PM
ame
got it:
myCtrl.InitializeAsUserControl(this.Page);