Load User Control Dynamically

  • Thread starter Thread starter Lou
  • Start date Start date
L

Lou

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
 
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
 
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
 
Back
Top