Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx_beginners thread: adding controls to an ascx page dynamically


Message #1 by "Jason Pluis" <jason.pluis@a...> on Mon, 17 Mar 2003 23:42:17
Hi friends,

I am dynamically adding a control to my aspx page. When the dynamically 
added control is called, it is passed a variety of parameters. 

What i need to happen is: the ascx should use the parameters to determine 
what kind of web form control to add (textbox, checkbox, readiobutton 
etc... This will have to be done dynamically. The web form control(s) 
would be added dynamically to another placeholder that exists inside the 
ascx page. 

How do I go about this??? I have created the aspx page so that i can 
dynamically add the user control. And I have created a partial ascx page, 
but can't figure out how to add the web form controls to the ascx's 
placeholder when the ascx page is called.

Cheers,
JasonP
Canberra, Australia

===ASPX CODE===
...
<snippet>
public void insertMetaEditCtrl(
	string id, 
	string type, 
	string subtype, 
	string enums, 
	string maxlength, 
	string currentvalue, 
	string schema,
	string ns
	)
{
	MetaDataEditControl ctrl = (MetaDataEditControl)Page.LoadControl
("metadata_edit.ascx");
	ctrl.Id = id;
	ctrl.Type = type;
	ctrl.Subtype = subtype;
	ctrl.Enums = enums;
	ctrl.MaxLength = maxlength;
	ctrl.Currentvalue = currentvalue;
	ctrl.Schema = schema;
	ctrl.Ns = ns;
	MetaEditPlaceHolder.Controls.Add(ctrl);
}
</snippet>
...


===ACSX CODE===
<%@ Control Language="cs" AutoEventWireup="false" 
ClassName="MetaDataEditControl" %>

<script language="cs" runat="server">
	
	private string id = "";
	private string type = "";
	private string subtype = "";
	private string enums = "";
	private string maxlength = "";
	private string currentvalue = "";
	private string schema = "";
	private string ns = "";

	public string Id 
	{ 
		get{return id;} 
		set{id = value;} 
	} 
	public string Type 
	{ 
		get{return type;} 
		set{type = value;} 
	}
	public string Subtype 
	{ 
		get{return subtype;} 
		set{subtype = value;} 
	}
	public string Enums 
	{ 
		get{return enums;} 
		set{enums = value;} 
	} 
	public string MaxLength 
	{ 
		get{return maxlength;} 
		set{maxlength = value;} 
	} 
	public string Currentvalue 
	{ 
		get{return currentvalue;} 
		set{currentvalue = value;} 
	} 
	public string Schema 
	{ 
		get{return schema;} 
		set{schema = value;} 
	} 
	public string Ns 
	{ 
		get{return ns;} 
		set{ns = value;} 
	} 
</script>

<tr>
	<th valign="top"><%=Id%>:</th>
	<td valign="top">- <asp:PlaceHolder id="element" 
runat="server" /></td>
</tr>
Message #2 by "Peter Lanoie" <planoie@n...> on Tue, 18 Mar 2003 12:18:05 -0500
Jason,

After typing the response below, I reread your post (and also realized there
was code with it :-).  At first, I didn't quite understand what your problem
was, but I think I get it now.

You are loading up a user control in a web form, then need to have the
control build itself based on the values of the properties you set after you
load it.  I see that your User control contains the HTML and the
placeholder.  You should be able to override the "Render" method in the User
Control such that you can then have it build itself based on the properties.
Otherwise, you could create a routine that you call after you set the
properties...

ASPX:	...
	ctrl.Ns = ns;
	ctrl.BuildMe(); //  <-- Call your "build" method after setting all props
	MetaEditPlaceHolder.Controls.Add(ctrl);

ASCX:
	public void BuildMe(void){
		/*
		Create your controls based on the property settings here
		and add them to the "element" placeholder.
		this.element.Controls.Add(textbox/checkbox/readiobutton/etc.)
		*/
	}

(Please excuse incorrect C# syntax, I'm a VB lackey)

Hopefully that will help you out.

<previous response>
I'm not sure if I understand your question.  You have a web form (ASPX) and
a user control (ASCX).  I'm not clear as to which controls you are trying to
place into which placeholder?  ASPX controls into a placeholder on the ASCX
or the other way around?

If you want to put controls into an ASCX, when you add that control onto the
web form it's just a matter of myControl.Controls.Add(anothercontrol).

If you want to be able to have the ASCX push stuff onto the parent form, the
easiest way I've found is to tie it together with a placeholder (which might
be what you're trying to do).  Create a PlaceHolder on the ASPX.  Create a
PlaceHolder on the ASCX.  When you load the ASCX into the ASPX, tie them
together.

Code in ASPX:

	objMyUserControl = LoadControl("myUserControl.ascx")
	Me.MyWebFormPlaceHolder = objMyUserControl.MyUCPlaceHolder

Now you should be able to have code in the ASCX put controls into the
UserControl placeholder that's hooked into the Web Form placeholder.
</previous response>

Peter

-----Original Message-----
From: Jason Pluis [mailto:jason.pluis@a...]
Sent: Monday, March 17, 2003 23:42
To: aspx_beginners
Subject: [aspx_beginners] adding controls to an ascx page dynamically


Hi friends,

I am dynamically adding a control to my aspx page. When the dynamically
added control is called, it is passed a variety of parameters.

What i need to happen is: the ascx should use the parameters to determine
what kind of web form control to add (textbox, checkbox, readiobutton
etc... This will have to be done dynamically. The web form control(s)
would be added dynamically to another placeholder that exists inside the
ascx page.

How do I go about this??? I have created the aspx page so that i can
dynamically add the user control. And I have created a partial ascx page,
but can't figure out how to add the web form controls to the ascx's
placeholder when the ascx page is called.

Cheers,
JasonP
Canberra, Australia

===ASPX CODE===
...
<snippet>
public void insertMetaEditCtrl(
	string id,
	string type,
	string subtype,
	string enums,
	string maxlength,
	string currentvalue,
	string schema,
	string ns
	)
{
	MetaDataEditControl ctrl = (MetaDataEditControl)Page.LoadControl
("metadata_edit.ascx");
	ctrl.Id = id;
	ctrl.Type = type;
	ctrl.Subtype = subtype;
	ctrl.Enums = enums;
	ctrl.MaxLength = maxlength;
	ctrl.Currentvalue = currentvalue;
	ctrl.Schema = schema;
	ctrl.Ns = ns;
	MetaEditPlaceHolder.Controls.Add(ctrl);
}
</snippet>
...


===ACSX CODE===
<%@ Control Language="cs" AutoEventWireup="false"
ClassName="MetaDataEditControl" %>

<script language="cs" runat="server">

	private string id = "";
	private string type = "";
	private string subtype = "";
	private string enums = "";
	private string maxlength = "";
	private string currentvalue = "";
	private string schema = "";
	private string ns = "";

	public string Id
	{
		get{return id;}
		set{id = value;}
	}
	public string Type
	{
		get{return type;}
		set{type = value;}
	}
	public string Subtype
	{
		get{return subtype;}
		set{subtype = value;}
	}
	public string Enums
	{
		get{return enums;}
		set{enums = value;}
	}
	public string MaxLength
	{
		get{return maxlength;}
		set{maxlength = value;}
	}
	public string Currentvalue
	{
		get{return currentvalue;}
		set{currentvalue = value;}
	}
	public string Schema
	{
		get{return schema;}
		set{schema = value;}
	}
	public string Ns
	{
		get{return ns;}
		set{ns = value;}
	}
</script>

<tr>
	<th valign="top"><%=Id%>:</th>
	<td valign="top">- <asp:PlaceHolder id="element"
runat="server" /></td>
</tr>

Message #3 by "Jason Pluis" <jason.pluis@a...> on Tue, 18 Mar 2003 23:03:56
Thanks Peter,

I ended up using the method you siggested that involved calling a routine 
after all the properties of the ascx file were set. I had a bit of a drama 
trying to figure out *how/where* to call that routine, but ended up 
figuring out that if I put <%BuildMe()%> immediately before the html code 
that it worked fine.

Thanks for your help.
JasonP.
Message #4 by "Jason Pluis" <jason.pluis@a...> on Tue, 18 Mar 2003 23:10:17
Aaaah, I just noticed the line:
... ctrl.BuildMe();  ...
that belongs in the aspx file. I didn't know how to do that. (I am still a 
C#/ASP.net newbie.) I think I'll move my <%BuilkdMe()%> code out of the 
ascx page and place your suggested line into the aspx page. Much neater.

Cheers,
JasonP.

  Return to Index