Initialising UserControls

D

Dave

If I have a user control that I want to initialize with a parameter;
that cannot be changed during the lifecyle of the control; like what you
can normally easily accomplish with class constructors.

I'm dynamically adding the control to the page, so

1 Control myControl = LoadControl(theControl.aspx);
2 //this line blank =)
3 Placeholder.Controlsl.Add(mycontrol);

I can easily set a property of the control at line 2 with a
mycontrol.propertyname=X and that works. But there is nothing stopping
me from changing the that property over and over again and that isn't
the behaviour I want to allow.

Is that the only way?? Or am I missing something??

Thanks,
Dave
 
D

Davide Vernole [MVP]

Dave said:
If I have a user control that I want to initialize with a parameter;
that cannot be changed during the lifecyle of the control; like what
you can normally easily accomplish with class constructors.

I'm dynamically adding the control to the page, so

1 Control myControl = LoadControl(theControl.aspx);
2 //this line blank =)
3 Placeholder.Controlsl.Add(mycontrol);

I can easily set a property of the control at line 2 with a
mycontrol.propertyname=X and that works. But there is nothing stopping
me from changing the that property over and over again and that isn't
the behaviour I want to allow.

Is that the only way?? Or am I missing something??

Thanks,
Dave

You could insert another property that indicate the state of your main
property. If this indicate an initialization, your main property never
change. Like this:

private string _mainProp;
private bool _isInit = false;

public string MainProp
{
get
{
return this._mainProp;
}
set
{
if(!this._isInit)
{
this._mainProp = value;
this._isInit = true;
Session["Initialize"] = true:
}
}
}

public bool IsInit
{
get
{
return this._isInit;
}
}

To work properly you need to insert this piece of code in your Page_Load
method (in the ascx control):

if(!IsPostaBack)
{
Session["Initialize"] = null;
}
else
{
if(Session["Initialize"] != null)
this._isInit = (bool)Session["Initialize"];
}

so you can persist the value across postback event.

HTH
 
J

Justin Beckwith

One option is check to see if a value is already set, and if it is,
throw an IvalidOperationException. So in the accesor that sits in
your user control, lets assume you're using a property called Text.

public property text As String
Get
return object.Text
End Get
Set (Byval value As String)
if object.Text <> "" then
object.Text = Value
Else
Throw InvalidOperationException("The value of this property
can only be set once.")
End If
End Set
End Property

Not overly elegant, but it prevents the value from being set twice.
 

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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top