Still Stumped on passing property to dynamically loaded UC

D

darrel

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(customContentControl)
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/d...guide/html/cpconexposingpageletproperties.asp

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

-Darrel
 
M

Matt Berther

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(customContentControl);
}

Hope this helps...
 
D

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.

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
 
T

Trevor Benedict R

Have you tried

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

Trevor Benedict R
MCSD
 
M

Matt Berther

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? ;)
 
D

darrel

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
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top