LoadControl by type

A

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 ?
 
H

Harald M. Genauck

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
 
A

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.
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" ?
 
H

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?
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top