|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
LoadControl by type----- 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 ?
Show quote
Hide quote
> Instead of loading a User Control by filename No, there is no action missing. Something else is missing, the path. > > ----- > 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 ? 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 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 Control,>> The ascx-file contains information like 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" ? > 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 LoadControl's purpose is to load an UserControl including the HTML >>> 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" ? 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
Control disappears
DropDownList Input Alternative to asp:MenuControl? Upload Excel Spreadsheet GridView Edit Template Control Style Page Panels Drag 'n Drop support for custom asp.net catalogpart/catalogzone. How to prevent JavaScript conflict in custom server control (when added more than once to a page) Control disappears Enter key as select/submit button |
|||||||||||||||||||||||