Custom Server Control Property Validation Issue

J

Jim-M

I've been developing a custom server control that composites a Calendar
control and several other controls. In this class, I have two properties
that can be set either at designtime or at runtime.

The two properties are of Integer type and represent the minimum and maximum
number of detail lines that can be shown. I need to provide some validation
routines for these properties whenever they're set. For example, property
MinCalendarDateLines must be > 0 and <= the value of MaxCalendarDateLines.

I've added checks within the Set clause of the property definition (see code
below). These work great, and everything works in Design mode and at
runtime. However, once the properties are set, I'm running into problems
because of the default values I've set for both properties. The default
values (3 for both properties) are set so when the control loads, both
values will be prepopulated with this value. Simple enough.

Now the problem I'm experiencing comes in: When I set the "Max" and the
"Min" property both to 2 in design mode, problems set in. If I rebuild the
project or do anything else to force the designer to refresh (like switch to
HTML mode and the back to Design mode), rendering of the control fails with
an error block "Error Creating Control". Hovering over the error block
brings up a ToolTip that says "'2' could not be set on property
'MaxCalendarDateLines'".

Obviously, when the page is loaded into the designer, the default values of
3 are the current values of the private property members. The designer is
setting the MaxCalendarDateLines property first (setting it to 2 while the
MinCalendarDateLines value is still the default 3). I can't figure out how
to get around this problem.

(I have tried moving the initialization of _MinCalendarDateLines and
_MaxCalendarDateLines to the contructor, but the problem persists.)

I've also considered writing a customer Designer for this control to
validate the values of these properties within the designer. But that would
only provide validation support at design time.

Here's my current code for these properties:
'-------------------------------------------------------
<Category("Appearance"), _
Description("Minimum number of CalendarDate entries to show per day."), _
DefaultValue(3)> _
Public Property MinCalendarDateLines() As Integer
Get
Return _MinCalendarDateLines
End Get
Set(ByVal Value As Integer)
If _MinCalendarDateLines = Integer.MinValue Then
_MinCalendarDateLines = 3
End If
If Value > _MaxCalendarDateLines Then
Throw New ArgumentException( _
String.Format("Must be less than or equal to
MaxCalendarDateLines ({0})", _
_MaxCalendarDateLines), "MinCalendarDateLines")
ElseIf Value < 0 Then
Throw New ArgumentException("Must be non-negative integer",
"MinCalendarDateLines")
Else
_MinCalendarDateLines = Value
End If
_MinCalendarDateLines = Value
End Set
End Property
Private _MinCalendarDateLines As Integer = 3

<Category("Appearance"), _
Description("Maximum number of CalendarDate entries to show per day."), _
DefaultValue(3)> _
Public Property MaxCalendarDateLines() As Integer
Get
Return _MaxCalendarDateLines
End Get
Set(ByVal Value As Integer)
If _MaxCalendarDateLines = Integer.MaxValue Then
_MaxCalendarDateLines = 3
ElseIf Value < _MinCalendarDateLines Then
_MaxCalendarDateLines = _MinCalendarDateLines
Throw New ArgumentException( _
String.Format("Must be equal to or greater than
MinCalendarDateLines ({0})", _
_MinCalendarDateLines), "MaxCalendarDateLines")
ElseIf Value < 1 Then
_MaxCalendarDateLines = 1
Throw New ArgumentException("Must be equal to or greater than 1",
"MaxCalendarDateLines")
Else
_MaxCalendarDateLines = Value
End If
_MaxCalendarDateLines = Value
End Set
End Property
Private _MaxCalendarDateLines As Integer = 3
'-------------------------------------------------------

Any assistance would be MOST appreciated!

--- Jim ---
 
J

Jim-M

Thanks Scott, those attributes did the trick. I NEVER would have pulled
that one out of Help without a nudge in the right direction. I'll
research them later. Thanks for the quick fix!

(and also thanks for pointing out that I needed to stash the values in
ViewState...these properties were recently added and I forgot to deal
with the state bag implementation)

--- Jim ---
 
J

Jim-M

Turns out that in this case, DesignerSerializationVisibility.Visible did
the trick. PersistenceMode.Attribute wasn't necessary, but I'm glad to
understand PersistenceMode a little better...your pointing it out forced
me to do a little more homework.

Thx again,

--- Jim ---
 
S

Scott Mitchell [MVP]

Jim-M said:
Thanks Scott, those attributes did the trick. I NEVER would have pulled
that one out of Help without a nudge in the right direction. I'll
research them later. Thanks for the quick fix!

Glad to hear I could help. Might I highly recommend that you pick up a
copy of Developing Microsoft ASP.NET Server Controls and Components if
you don't already own a copy?

Link to Buy:
http://www.amazon.com/exec/obidos/ASIN/0735615829/4guysfromrollaco

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top