Logo 
Search:

Asp.net Forum

Ask Question   UnAnswered
Home » Forum » Asp.net       RSS Feeds

LoadControl

  Asked By: Maria    Date: Aug 14    Category: Asp.net    Views: 901
  

If we use Load control, perhaps (as in the GotDotNetMenuSource example available all over the place)


cwd.cwdmenu.UI.Components.Navigation.MenuCollapsing adsMenu = new cwd.cwdmenu.UI.Components.Navigation.MenuCollapsing();
adsMenu = (cwd.cwdmenu.UI.Components.Navigation.MenuCollapsing)LoadControl("UI/Components/Navigation/Menu/Menu.ascx");

we can then programatically add this to say, a panel, in the 'display page'

panel1.Controls.Add(adsMenu)

[[
OR - if we were to add it to the 'display page' using the designer/html
<%@ Register TagPrefix="cwd" TagName="ads" src="UI/Components/Navigation/Menu/Menu.ascx" %>
..... and then add the tag.

OR .... if we have overridden Render - we no longer have an ascx file as the src - so we use
<%@ Register TagPrefix="ads" Namespace="cwd.cwdmenu.UI.Components.Navigation" Assembly = "cwdmenu" %>
.... and then add the tag
]]

(see ms-help://MS.VSCC/MS.MSDNVS/cpguide/html/cpconcompositecontrolsample.htm)

This much I am fine with.

But look above again, in the first instance when we want to add the menu to 'display page' programatically we use LoadControl ..... but what if we have Overidden Render in a custom control which has been built programmatically and doesn't have an ascx file .... how can we use Load Control without an ascx file ????? LoadControl only has one constructor.

The answer maybe to simply leave out the line
adsMenu = (cwd.cwdmenu.UI.Components.Navigation.MenuCollapsing)LoadControl("UI/Components/Navigation/Menu/Menu.ascx");
I could presume the actual function of LoadControl as being specific to load the ascx file, the html - seeing as the control has already been instantiated in the line above it - after all we don't have LoadControl with a Label or Panel :: but then again you would have thought that the base classes Render method would do this automatically.
So what exactly in the function of LoadControl - what exactly does it do ?
Note :: I haven't actually tried this yet as I haven't rebuilt my control and ridded myself of the ascx file as yet - I am asking to check with anyone if they have done something similar.

Has anyone come across this yet ??? Maybe I am missing something ?

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Mildred Bailey     Answered On: Aug 14

This is a note in answer  to my previously posted question - sent for tips incase anyone comes across the 'problem' in future.

It's not a problem, it was me. The LoadControl DOES actually mean Load the ascx.

If you want to create controls  entirely in code then DO NOT use System.Web.UI.UserControl
(I was sure that I had overidden Render in UserControl in a previous occasion, but I can't seem to do it now even though the docs tell me I should be able to)

Use a System.Web.UI.WebControls.WebControl and overidding Render is easy.

Notes :: If you don't want to overide render in one of these things but wish merely to add  controls to it then use this set up ::


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace cntrls
{
/// <summary>
/// Summary description for cc.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:cc runat=server></{0}:cc>")]
public class cc : System.Web.UI.WebControls.WebControl
{
private string text;
protected System.Web.UI.WebControls.Label L1;

[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}

/// <summary>
/// Render this control  to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
// protected override void Render(HtmlTextWriter output)
// {
// output.Write(Text);
// }
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
L1 = new Label();
L1.Text = "hello from CC3";
this.Controls.Add(L1);

}
}
}


Other worthy notes ::

Its easy to include controls of the same type simply by instantiating them (obviously adding UserControls ith ascx  files defeates the object as you can't wrap the whole thing nicely in a dll - if indeed it's even possible (possibly ?? I don't even care and ain't bothering to find out))

ALSO NOTE :: When rebuilding and adding/subracting controls I found that sometimes - although I had added them to the toolbox, etc ... that the extra ones were not being allowed to be dragged to the aspx page.
Solution was to dereference the library and then reference it. Done.

Also - VS.NET has 'em all stuck in mem (or somewhere) and they're a bugger to get rid of - just reset the toolbox between attempts.


I now have everything I need to make controls available to the toolbox.

 
Didn't find what you were looking for? Find more on LoadControl Or get search suggestion and latest updates.

Related Topics:



Tagged:  

 

Related Post