473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Still Stumped on passing property to dynamically loaded UC

I'm still a bit stumped on how to load a usercontrol, and then pass a
property value (or variable value) to it.

Here's what I'm using to load the UC:

localCUstomControl.ascx.vb
-------------------------------------
sub initializeExtraControls()
panel_controlHolder.visible = True
customContentControl = CType(Page.LoadControl("../usercontrols/" &
customContent &".ascx"),UserControl)
panel_controlHolder.Controls.Add(customContentCont rol)
End Sub
-------------------------------------

That works just fine, loading the UC. Now, where I get stuck is how do I
pass a property value to that UC?

In the UC I am loading I have this:
usercontrol.ascx.vb
-------------------------------------
public property sampleProperty
-------------------------------------

I've tried just using customContentControl.sampleProperty = "whatever" but
that doesn't work, as sampleProperty is not a mamber of
'system.web.ui.usercontrol'

I've been reading and re-reading this MSDN article:
http://msdn.microsoft.com/library/de...properties.asp

But I'm guessing I'm missing something obvious.

-Darrel
Nov 19 '05 #1
5 1344
Hello darrel,

The way to make this happen is to define an interface or a base class that
your user control derives from and then cast to that type in your method.

[C#]
public class MyControlBase : System.Web.UI.UserControl
{
private string myProp;

public string SampleProperty
{
get { return myProp; }
set { myProp = value; }
}

}

public class MyConcreteControl : MyControlBase
{
}

Then in LoadCustomControl:

private void InitializeExtraControls()
{
panel_controlHolder.Visible = true;
MyControlBase customContentControl = (MyControlBase)Page.LoadControl("../usercontrols/"
+ customContent + ".ascx");
customContentControl.SampleProperty = "foo";
panel_controlHolder.Controls.Add(customContentCont rol);
}

Hope this helps...

--
Matt Berther
http://www.mattberther.com
I'm still a bit stumped on how to load a usercontrol, and then pass a
property value (or variable value) to it.

Here's what I'm using to load the UC:

localCUstomControl.ascx.vb
-------------------------------------
sub initializeExtraControls()
panel_controlHolder.visible = True
customContentControl = CType(Page.LoadControl("../usercontrols/" &
customContent &".ascx"),UserControl)
panel_controlHolder.Controls.Add(customContentCont rol)
End Sub
-------------------------------------

That works just fine, loading the UC. Now, where I get stuck is how do
I pass a property value to that UC?

In the UC I am loading I have this:

usercontrol.ascx.vb
-------------------------------------
public property sampleProperty
-------------------------------------
I've tried just using customContentControl.sampleProperty = "whatever"
but that doesn't work, as sampleProperty is not a mamber of
'system.web.ui.usercontrol'

I've been reading and re-reading this MSDN article:

http://msdn.microsoft.com/library/de...ry/en-us/cpgui
de/html/cpconexposingpageletproperties.asp

But I'm guessing I'm missing something obvious.

-Darrel


Nov 19 '05 #2
> The way to make this happen is to define an interface or a base class that
your user control derives from and then cast to that type in your method.


Is that the only way? I was using interfaces for a bit, but we decided it
was overkill for what we were trying to accomplish. I'll take a second look
at them, though.

It seems odd that it's so incredibly easy to set a property when loading a
control in the ASPX file, but considerably more complicated when doing it
from the codebehind file. ;o)

-Darrel
Nov 19 '05 #3
Have you tried

CType(customContentControl, customContent).SomeProperty = value or a
variation of this.

Trevor Benedict R
MCSD

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #4
Hello darrel,
Is that the only way? I was using interfaces for a bit, but we decided
it was overkill for what we were trying to accomplish. I'll take a
second look at them, though.
An interface would work as well.
It seems odd that it's so incredibly easy to set a property when
loading a control in the ASPX file, but considerably more complicated
when doing it from the codebehind file. ;o)


Amazing what the ASP.NET compilation model hides from you, isnt it? ;)

--
Matt Berther
http://www.mattberther.com

Nov 19 '05 #5
> CType(customContentControl, customContent).SomeProperty = value or a
variation of this.


What is 'customContent' in the above example? Is that a class/namespace I'd
create?

-Darrel
Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bryan Martin | last post by:
I have a object that is created in a seperate domain which needs to be passed back to the parent class. Because this object is created in a seperate domain if I try to pass the object back to the...
4
by: Ahmet | last post by:
Hi all; I have one application in which I read form names from database to be opened. I open form with the code below, and call its show method for form to be shown but before this, I must set...
7
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
1
by: Josh | last post by:
Hi Guys, I have been having a big problem with trying to pass parameters into a user control when the user control is dynamically loaded into a placholder. I am developing in c#. I have get...
2
by: darrel | last post by:
If I want to pass data to a UC via codebehind on the page, does the UC also have to be dynamically loaded, or can I pass data progamatically to a UC that is loaded via the ASPX page (using a UC...
3
by: voro.cibus | last post by:
I have been reading up on this all day, and I can't find the answer (or more likely, don't understand the answers I have found) to my problem. I have a table that stores the name of my ascx page....
5
by: sfeher | last post by:
Hi All, I need to call a function(loaded with appendChild) for which I have the name as a string. .... var fnName = 'fn1'; var call = fnName + '('+ param +' )'; eval(call);
1
by: =?Utf-8?B?ZGF2ZQ==?= | last post by:
I have an asp.net project with a business layer (project) that has a class called references. It loads up a data set and stores in cache with the following code. _cached =...
2
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.