Confusion in testing dynamic loading of controls


Lloyd Sheen

Ok I have created a small testbed site for testing and I am have a problem
with the terms Usercontrol, Webcontrol, etc.

What I need to do in the long run is have a page using AJAX and having
dynamically loaded controls on the page.

So what I did was first create a page with two buttons. First button will
load a Label control into a placeholder. After execution and clicking the
button the label shows in the page.

Next I create a WebUserControl (now is that a UserControl or a WebControl
????) do the same with the other button. The WebUserControl has just a
Label so it should show the same as the first button behaviour I would
think. But nothing shows in the placeholder.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Session("Type") = "One"
Dim ctl As WebControl
Dim lbl As New Label
lbl.Text="This is label1"
ctl = lbl
ctl.Visible = True
End Sub

Above shows the label no problem

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click
Session("Type") = "Two"
Dim ctl As UserControl
ctl = New WebUserControl2
ctl.Visible = True
End Sub

This code shows nothing on the resulting page.

I need to get this working with AJAX but cannot get a simple WebUserControl
on the page.

Any ideas??

Lloyd Sheen

Rick Strahl [MVP]

Hi Llyod,

A UserControl is a visually composed control (typically stored in an .ASCX
markup file). A WebControl refers to a full ASP.NET server control like
Label, TextBox, GridView etc. but can also be a custom created control -
anything that inherits from Control qualifies.

UserControls and WebControls load differently because UserControls typically
are loaded by their filename with ASP.NET handling the parsing of the
control at runtime and expanding the template and then instantiating it,
whereas a WebControl is an already compiled control that can be just

So in the second example, what are you actually trying to do? If you're
trying to load a User Control you should use:

UserControl Ctl = this.LoadControl("SomeControl.ascx");

to ensure you get the control loaded properly.

+++ Rick ---

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