User control Example needed
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngbeta' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Michael Bunger
Hi All,

Does anyone have a working example of programatically loading a user control
in code behind, then setting some properties of the user control, where the
user control itself is written with code behind?

I am having all kinds of trouble getting this to work and I would greatly
appreciate an example with all the pieces of code -- user control GUI, user
control code-behind class, container GUI, container code-behind class.

Any help will be greatly appreciated.

Thanks,
Michael Bunger
Reply to this message...
 
    
Nathen Grass
Here's an example in C#. Since C# doesn't inherently support late-binding
you have to use reflection to set the property of the user control
programmatically. The example includes Test1.aspx, its code-behind Test1.cs,
the user control Test1UC.ascx, and its code-behind Test1UC.cs. The user
control exposes a public property called TextValue. The aspx code-behind
dynamically loads the user control, sets the user control's property using
reflection, and then adds it to the controls collection of the Panel in the
aspx page. Here's the code:

==== Test1.aspx ====
<%@ Page Language="C#" src="Test1.cs" inherits="Test1"%>
<html>
<head>
</head>
<body>
Loading a User Control Programmatically
<br>
<br>
<asp:Panel id="Panel1" runat="server"/>
</body>
</html>
==== End Test1.aspx ====

==== Test1.cs ====
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

public class Test1 : Page
{
protected Panel Panel1;
protected void Page_Load(object s, EventArgs e)
{
UserControl uc = LoadControl("Test1UC.ascx");
Type ucType = uc.GetType();
PropertyInfo ucType_TextValue = ucType.GetProperty("TextValue");
ucType_TextValue.SetValue(uc, "Property set programmatically",
BindingFlags.SetProperty, null, null, null);
Panel1.Controls.Add(uc);
}
}
==== End Test1.cs ====

==== Test1UC.ascx ====
<%@ Control language="C#" inherits="Test1UC" src="Test1UC.cs"%>

User Control Label:<br>
<asp:Label id="Label1" runat="server" />
==== End Test1UC.ascx ====

==== Test1UC.cs ====
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class Test1UC : UserControl
{
protected Label Label1;
private string _TextValue;

public string TextValue
{
get { return _TextValue; }
set { _TextValue = value; }
}

protected void Page_Load(object s, EventArgs e)
{
Label1.Text = TextValue;
}
}
==== End Test1UC.cs ====

-----Original Message-----
From: Michael Bunger
To: aspngbeta
Sent: 5/2/2001 10:29 AM
Subject: [aspngbeta] User control Example needed

Hi All,

Does anyone have a working example of programatically loading a user
control in code behind, then setting some properties of the user
control, where the user control itself is written with code behind?

I am having all kinds of trouble getting this to work and I would
greatly appreciate an example with all the pieces of code -- user
control GUI, user control code-behind class, container GUI, container
code-behind class.

Any help will be greatly appreciated.

Thanks,
Michael Bunger

Reply to this message...
 
    
dave 123aspx.com (VIP)
Hi Michael,
This is a common question, in fact I wrote a quick little article (mainly because I kept forgetting how to do it) and posted it at aspfree.com.

Here it is:
http://www.aspfree.com/aspnet/usercontrol.aspx

Cheers!
dave
PS: If you want more info:
here is a listing of Usercontrol tutorials I've found:
http://www.123aspx.com/directory.asp?dir=10

and here is Usercontrol code snippets I've added to my directory:
http://www.123aspx.com/directory.asp?dir=20

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.123aspx.com
The Largest ASP.NET Directory!
Find the latest ASP.NET resources --
Subscribe to our newsletter!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
----- Original Message -----
From: Michael Bunger
To: aspngbeta
Sent: Wednesday, May 02, 2001 9:29 AM
Subject: [aspngbeta] User control Example needed

Hi All,

Does anyone have a working example of programatically loading a user control in code behind, then setting some properties of the user control, where the user control itself is written with code behind?

I am having all kinds of trouble getting this to work and I would greatly appreciate an example with all the pieces of code -- user control GUI, user control code-behind class, container GUI, container code-behind class.

Any help will be greatly appreciated.

Thanks,
Michael Bunger
| [aspngbeta] member Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT
Reply to this message...
 
    
Stuart C. Salsbury
Michael,

I'll try to post some code that might help... it won't have .ascx nor .aspx
files though, rather code-only versions of a control and a page.

In the meantime, I'd point out that your characterization of GUI vs.
code-behind is not truly accurate if your intention was to indicate .aspx
vs. .cs/.vb files. The designer files (.aspx and .ascx) are really just
ways of declaratively creating classes that derive from System.Web.UI.Page
and System.Web.UI.Control.... I think that whether you choose to use
.aspx/.ascx or go with pure csharp of vb is more a matter of what importance
you place on reusability, build-time type safety, and speed.

Thus, the GUI (aka user-visible HTML elements) can come either from .aspx or
.cs/.vb files (though it is unarguably easier to write big chunks of HTML in
a .aspx or .ascx file).

When the framework encounters an .aspx file, it basically does what the
CreateChildControls method Page does (because Page is a descendant of
Control). It adds a LiteralControl element to its Controls collection in
order to contain all of the HTML that it finds up until it finds its first
server control (runat&#"s;erver"). It adds the found HTML as a
LiteralControl, then (oversimplified) parses and adds the server control
(there are now 2 child controls, the LiteralControl that holds all of the
straight HTML and some other server control that does something more
interesting). Then it addes another LiteralControl if the next stuff that
it finds isn't a server control... then it adds the next server control,
[loop here] until the end of the file is reached. Thus, you end up with a
collection of "child controls" which is a bunch of LiteralControls that are
interspersed among more interesting server controls. When the render method
is called (near the end of Http request processing), each control is asked
to render itself into the output stream in order from first to last. This
behavior is overridable (you can override the Render method to do something
totally different if you wish). Thus HTML lands in the browser.

Finally, note that the .aspx file may state to the framework (via the
inherits attribute in the Page or UserControl directive) that it will
inherit from some class. When you write a page or control that uses the
declarative syntax (.aspx or .ascx), youll have an object model of

Page (or Control)
|
CodeBehindClass
|
ASXXClass (this is the class that ultimately serves the page (or
control) function)

whereas if you do not use a declarative page, you have

Page (or Control)
|
CodeBehindClass (this is the class that ultimately serves the
page (or control) function)

I hope I haven't just further complicated your life nor blown a bunch of hot
air. I think that getting this point is important when working with
code-behind files. Perhaps this will make more sense with the example.

More to come,
Stuart Salsbury
Ernst & Young LLP

-----Original Message-----
From: Michael Bunger [mailto:Click here to reveal e-mail address]
Sent: Wednesday, May 02, 2001 10:29 AM
To: aspngbeta
Subject: [aspngbeta] User control Example needed

Hi All,

Does anyone have a working example of programatically loading a user control
in code behind, then setting some properties of the user control, where the
user control itself is written with code behind?

I am having all kinds of trouble getting this to work and I would greatly
appreciate an example with all the pieces of code -- user control GUI, user
control code-behind class, container GUI, container code-behind class.

Any help will be greatly appreciated.

Thanks,
Michael Bunger
Reply to this message...
 
    
Stuart C. Salsbury
It looks like Nathen's example may suit the original question better than
mine would have (since it uses .as_x files). If anyone wants a simple
code-only example, say so and I'll clean it up -- you know, take out all of
the profanity ; ) -- and then post it.

Regards,
Stuart Salsbury
Ernst & Young LLP

-----Original Message-----
From: Nathen Grass [mailto:Click here to reveal e-mail address]
Sent: Wednesday, May 02, 2001 7:37 PM
To: aspngbeta
Subject: [aspngbeta] RE: User control Example needed

Here's an example in C#. Since C# doesn't inherently support late-binding
you have to use reflection to set the property of the user control
programmatically. The example includes Test1.aspx, its code-behind Test1.cs,
the user control Test1UC.ascx, and its code-behind Test1UC.cs. The user
control exposes a public property called TextValue. The aspx code-behind
dynamically loads the user control, sets the user control's property using
reflection, and then adds it to the controls collection of the Panel in the
aspx page. Here's the code:

==== Test1.aspx ====
<%@ Page Language="C#" src="Test1.cs" inherits="Test1"%>
<html>
<head>
</head>
<body>
Loading a User Control Programmatically
<br>
<br>
<asp:Panel id="Panel1" runat="server"/>
</body>
</html>
==== End Test1.aspx ====

==== Test1.cs ====
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

public class Test1 : Page
{
protected Panel Panel1;
protected void Page_Load(object s, EventArgs e)
{
UserControl uc = LoadControl("Test1UC.ascx");
Type ucType = uc.GetType();
PropertyInfo ucType_TextValue = ucType.GetProperty("TextValue");
ucType_TextValue.SetValue(uc, "Property set programmatically",
BindingFlags.SetProperty, null, null, null);
Panel1.Controls.Add(uc);
}
}
==== End Test1.cs ====

==== Test1UC.ascx ====
<%@ Control language="C#" inherits="Test1UC" src="Test1UC.cs"%>

User Control Label:<br>
<asp:Label id="Label1" runat="server" />
==== End Test1UC.ascx ====

==== Test1UC.cs ====
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class Test1UC : UserControl
{
protected Label Label1;
private string _TextValue;

public string TextValue
{
get { return _TextValue; }
set { _TextValue = value; }
}

protected void Page_Load(object s, EventArgs e)
{
Label1.Text = TextValue;
}
}
==== End Test1UC.cs ====

-----Original Message-----
From: Michael Bunger
To: aspngbeta
Sent: 5/2/2001 10:29 AM
Subject: [aspngbeta] User control Example needed

Hi All,

Does anyone have a working example of programatically loading a user
control in code behind, then setting some properties of the user
control, where the user control itself is written with code behind?

I am having all kinds of trouble getting this to work and I would
greatly appreciate an example with all the pieces of code -- user
control GUI, user control code-behind class, container GUI, container
code-behind class.

Any help will be greatly appreciated.

Thanks,
Michael Bunger

Reply to this message...
 
    
David Ebbo
Actually, you should avoid doing this using reflection, as it is quite
slow and will not catch errors at compile times. The way to do this in
an early bound way is to compile you codebehind for the ascx and aspx in
the same assembly. This way, in Test1.cs, you can write:

protected void Page_Load(object s, EventArgs e)=20
{=20
Test1UC uc =3D (Test1UC) LoadControl("Test1UC.ascx");
uc.TextValue =3D "Property set programmatically2";
Panel1.Controls.Add(uc);=20
}

Much faster and cleaner. There is also a way to do something similar if
you don't use codebehind, but it's a bit hacky in beta 1 bits. It will
be better supported in beta 2.

David;

-----Original Message-----
From: Nathen Grass [mailto:Click here to reveal e-mail address]=20
Sent: Wednesday, May 02, 2001 4:37 PM
To: aspngbeta
Subject: [aspngbeta] RE: User control Example needed

Here's an example in C#. Since C# doesn't inherently support
late-binding=20
you have to use reflection to set the property of the user control=20
programmatically. The example includes Test1.aspx, its code-behind
Test1.cs, the user control Test1UC.ascx, and its code-behind Test1UC.cs.
The user control exposes a public property called TextValue. The aspx
code-behind dynamically loads the user control, sets the user control's
property using reflection, and then adds it to the controls collection
of the Panel in the aspx page. Here's the code:=20

=3D=3D=3D=3D Test1.aspx =3D=3D=3D=3D=20
<%@ Page Language=3D"C#" src=3D"Test1.cs" inherits=3D"Test1"%>=20
<html>=20
<head>=20
</head>=20
<body>=20
Loading a User Control Programmatically=20
<br>=20
<br>=20
<asp:Panel id=3D"Panel1" runat=3D"server"/>=20
</body>=20
</html>=20
=3D=3D=3D=3D End Test1.aspx =3D=3D=3D=3D=20

=3D=3D=3D=3D Test1.cs =3D=3D=3D=3D=20
using System;=20
using System.Web;=20
using System.Web.UI;=20
using System.Web.UI.WebControls;=20
using System.Reflection;=20

public class Test1 : Page=20
{ =20
protected Panel Panel1;=20
protected void Page_Load(object s, EventArgs e)=20
{=20
UserControl uc =3D LoadControl("Test1UC.ascx");=20
Type ucType =3D uc.GetType();=20
PropertyInfo ucType_TextValue =3D ucType.GetProperty("TextValue");=20
ucType_TextValue.SetValue(uc, "Property set programmatically",=20
BindingFlags.SetProperty, null, null, null);=20
Panel1.Controls.Add(uc);=20
} =20
}=20
=3D=3D=3D=3D End Test1.cs =3D=3D=3D=3D=20

=3D=3D=3D=3D Test1UC.ascx =3D=3D=3D=3D=20
<%@ Control language=3D"C#" inherits=3D"Test1UC" src=3D"Test1UC.cs"%>=20

User Control Label:<br>=20
<asp:Label id=3D"Label1" runat=3D"server" />=20
=3D=3D=3D=3D End Test1UC.ascx =3D=3D=3D=3D=20

=3D=3D=3D=3D Test1UC.cs =3D=3D=3D=3D=20
using System;=20
using System.Web;=20
using System.Web.UI;=20
using System.Web.UI.WebControls;=20

public class Test1UC : UserControl=20
{=20
protected Label Label1;=20
private string _TextValue;=20

public string TextValue=20
{=20
get { return _TextValue; }=20
set { _TextValue =3D value; }=20
}=20
=20
protected void Page_Load(object s, EventArgs e)=20
{=20
Label1.Text =3D TextValue;=20
} =20
}=20
=3D=3D=3D=3D End Test1UC.cs =3D=3D=3D=3D=20

=20

-----Original Message-----
From: Michael Bunger
To: aspngbeta
Sent: 5/2/2001 10:29 AM
Subject: [aspngbeta] User control Example needed

Hi All,
=20
Does anyone have a working example of programatically loading a user
control in code behind, then setting some properties of the user
control, where the user control itself is written with code behind?
=20
I am having all kinds of trouble getting this to work and I would
greatly appreciate an example with all the pieces of code -- user
control GUI, user control code-behind class, container GUI, container
code-behind class.
=20
Any help will be greatly appreciated.
=20
Thanks,
Michael Bunger

| [aspngbeta] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.asplists.com/asplists/aspngbeta.asp =3D JOIN/QUIT

Reply to this message...
 
    
Michael Bunger
Thanks all for the code examples -- I'm going to dive into them soon.

And, Stuart, you're right: I was a little sloppy with my wording using
"GUI" etc. But, thanks for the good detailed explanation it elicited.

Thanks,
Michael Bunger

-----Original Message-----
From: David Ebbo [mailto:Click here to reveal e-mail address]
Sent: Wednesday, May 02, 2001 10:18 PM
To: aspngbeta
Subject: [aspngbeta] RE: User control Example needed

Actually, you should avoid doing this using reflection, as it is quite
slow and will not catch errors at compile times. The way to do this in
an early bound way is to compile you codebehind for the ascx and aspx in
the same assembly. This way, in Test1.cs, you can write:

protected void Page_Load(object s, EventArgs e)
{
Test1UC uc = (Test1UC) LoadControl("Test1UC.ascx");
uc.TextValue = "Property set programmatically2";
Panel1.Controls.Add(uc);
}

Much faster and cleaner. There is also a way to do something similar if
you don't use codebehind, but it's a bit hacky in beta 1 bits. It will
be better supported in beta 2.

David;

-----Original Message-----
From: Nathen Grass [mailto:Click here to reveal e-mail address]
Sent: Wednesday, May 02, 2001 4:37 PM
To: aspngbeta
Subject: [aspngbeta] RE: User control Example needed

Here's an example in C#. Since C# doesn't inherently support
late-binding
you have to use reflection to set the property of the user control
programmatically. The example includes Test1.aspx, its code-behind
Test1.cs, the user control Test1UC.ascx, and its code-behind Test1UC.cs.
The user control exposes a public property called TextValue. The aspx
code-behind dynamically loads the user control, sets the user control's
property using reflection, and then adds it to the controls collection
of the Panel in the aspx page. Here's the code:

==== Test1.aspx ====
<%@ Page Language="C#" src="Test1.cs" inherits="Test1"%>
<html>
<head>
</head>
<body>
Loading a User Control Programmatically
<br>
<br>
<asp:Panel id="Panel1" runat="server"/>
</body>
</html>
==== End Test1.aspx ====

==== Test1.cs ====
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

public class Test1 : Page
{
protected Panel Panel1;
protected void Page_Load(object s, EventArgs e)
{
UserControl uc = LoadControl("Test1UC.ascx");
Type ucType = uc.GetType();
PropertyInfo ucType_TextValue = ucType.GetProperty("TextValue");
ucType_TextValue.SetValue(uc, "Property set programmatically",
BindingFlags.SetProperty, null, null, null);
Panel1.Controls.Add(uc);
}
}
==== End Test1.cs ====

==== Test1UC.ascx ====
<%@ Control language="C#" inherits="Test1UC" src="Test1UC.cs"%>

User Control Label:<br>
<asp:Label id="Label1" runat="server" />
==== End Test1UC.ascx ====

==== Test1UC.cs ====
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public class Test1UC : UserControl
{
protected Label Label1;
private string _TextValue;

public string TextValue
{
get { return _TextValue; }
set { _TextValue = value; }
}

protected void Page_Load(object s, EventArgs e)
{
Label1.Text = TextValue;
}
}
==== End Test1UC.cs ====

-----Original Message-----
From: Michael Bunger
To: aspngbeta
Sent: 5/2/2001 10:29 AM
Subject: [aspngbeta] User control Example needed

Hi All,

Does anyone have a working example of programatically loading a user
control in code behind, then setting some properties of the user
control, where the user control itself is written with code behind?

I am having all kinds of trouble getting this to work and I would
greatly appreciate an example with all the pieces of code -- user
control GUI, user control code-behind class, container GUI, container
code-behind class.

Any help will be greatly appreciated.

Thanks,
Michael Bunger

| [aspngbeta] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT

| [aspngbeta] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngbeta.asp = JOIN/QUIT

Reply to this message...
 
 
System.EventArgs
System.Reflection.BindingFlags
System.Reflection.PropertyInfo
System.Web.UI.Control
System.Web.UI.LiteralControl
System.Web.UI.Page
System.Web.UI.UserControl
System.Windows.Forms.UserControl




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com