Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Load User Control Dynamically

0 views
Skip to first unread message

Lou

unread,
Feb 9, 2009, 11:56:49 AM2/9/09
to
How Do I load a user control dynamically.
The control is in the applications folder as a .dll
A user selects the file from disk then I need to load it into a panel.

something like;
UserControl uc= Load(filename.dll);

Lou


Peter Duniho

unread,
Feb 9, 2009, 1:16:44 PM2/9/09
to

You'll need to look at the Assembly class. In particular, loading an
assembly and then creating instances of classes found in the assembly.

You may want to also look at AppDomains, so that you can load the assembly
in its own AppDomain. Otherwise, once the assembly is loaded, it cannot
be unloaded (you can't unload Assemblys, just AppDomains).

Pete

Lou

unread,
Feb 9, 2009, 2:08:27 PM2/9/09
to
Any Source code to look at. I am new to C#.

-Lou

"Peter Duniho" <NpOeS...@nnowslpianmk.com> wrote in message
news:op.uo3jp...@macbook-pro.local...

Peter Duniho

unread,
Feb 9, 2009, 2:33:30 PM2/9/09
to
On Mon, 09 Feb 2009 11:08:27 -0800, Lou <lou.g...@comcast.net> wrote:

> Any Source code to look at. I am new to C#.

I don't have any lying around myself. MSDN usually has some code
examples, and of course you can probably turn up useful examples using
Google.

Justin Dutoit

unread,
Feb 10, 2009, 12:19:00 AM2/10/09
to
Here's some of my old VB code:

Dim ctlNextSearch As Control = LoadControl("Search.ascx")
CType(ctlNextSearch, SearchControl).SearchString = "whatever"
CType(ctlNextSearch, SearchControl).SearchNumberForNext = someNumber
CType(ctlNextSearch, SearchControl).LastSearchNumber = anotherNumber
Controls.Add(ctlNextSearch)

The first and last lines are the basic Load and Add functionality, but if
you want to set some properties of the user control, use the lines that
begin with CType (. I think the C# would look like:

Control ctlNextSearch = LoadControl("Search.ascx");
(SearchControl)ctlNextSearch.SearchString = "whatever";
etc
Controls.Add(ctlNextSearch);

The vb code for the user control is below.
Hope this helps,
Justin Dutoit


<%@ Control ClassName="SearchControl" Language="VB" %>
<%@ Import Namespace="Quickshop" %>
<%@ OutputCache Duration="10" VaryByParam="*" %>

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


Public SearchString As String
Public SearchNumberForNext As Integer


Sub Page_Load(Sender As Object, E As EventArgs)

Dim ConnectionString = "provider=sqloledb.1;data
source=(local);Connect Timeout=30;database=H_justindutoit;User
ID=justindutoit;Password=paulpaul;Packet Size=4096;"
Dim Products As ProductsDB = New ProductsDB(ConnectionString)

MyDataGrid.DataSource = Products.SimpleSearch(SearchString)
MyDataGrid.DataBind

MyDataGridTitle.Text = "Results for '" & SearchString & "'"
ShowUplevelOrDownLevelColumns

If MyDataGrid.Items.Count = 0 Then _
NoResults.Visible = True Bookmark.Attributes("Name") = "Search" & SearchNumberForNext
LinkToNextBookmark.Attributes("href") = "#Search" & (SearchNumberForNext +
1)

End Sub


"Lou" <lou.g...@comcast.net> wrote in message
news:uSjbndti...@TK2MSFTNGP03.phx.gbl...

Lou

unread,
Feb 10, 2009, 9:04:25 AM2/10/09
to
LoadControl method is only for asp web pages.
But Thanks anyway.

Seems like it should be simple to load a user control from file.
In VB6 (Which is my background and why I'm haveing troubles)
it's easy:

Dim o As Object
Set o = CreateObject(ClassName)

That's it.

Confused!!!!!!
Any help is appreciated. I have spent two days and many hours googling
and no one has an answer.

-Lou

"Justin Dutoit" <an...@anon.com> wrote in message
news:upryg8zi...@TK2MSFTNGP02.phx.gbl...

0 new messages