Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Custom Server Control Property Validation Issue

Reply
Thread Tools

Custom Server Control Property Validation Issue

 
 
Jim-M
Guest
Posts: n/a
 
      05-11-2004
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 ---


 
Reply With Quote
 
 
 
 
Jim-M
Guest
Posts: n/a
 
      05-12-2004
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 ---

"Scott Mitchell [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jim, just some things off the top of my head to try... try adding the
> following attributes to both properties:
>
> PersistenceMode(PersistenceMode.Attribute),
>

DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)
>
> Also, I noticed your properties do not read/write their values to the
> ViewState state bag. You should consider doing this, because if you
> don't, if a developer were to set either of these properties
> programmatically, the changes would not be persisted across a

postback...
>
> --
>
> Scott Mitchell
> http://www.velocityreviews.com/forums/(E-Mail Removed)
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!



 
Reply With Quote
 
 
 
 
Jim-M
Guest
Posts: n/a
 
      05-12-2004
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 ---

"Scott Mitchell [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Jim, just some things off the top of my head to try... try adding the
> following attributes to both properties:
>
> PersistenceMode(PersistenceMode.Attribute),
>

DesignerSerializationVisibility(DesignerSerializat ionVisibility.Content)
>
> Also, I noticed your properties do not read/write their values to the
> ViewState state bag. You should consider doing this, because if you
> don't, if a developer were to set either of these properties
> programmatically, the changes would not be persisted across a

postback...
>
> --
>
> Scott Mitchell
> (E-Mail Removed)
> http://www.4GuysFromRolla.com
> http://www.ASPFAQs.com
> http://www.ASPMessageboard.com
>
> * When you think ASP, think 4GuysFromRolla.com!



 
Reply With Quote
 
Scott Mitchell [MVP]
Guest
Posts: n/a
 
      05-12-2004
Jim-M wrote:
> 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/AS...uysfromrollaco

Happy Programming!

--

Scott Mitchell
(E-Mail Removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
Reply With Quote
 
William Mansfield
Guest
Posts: n/a
 
      05-13-2004
Just a quick note to say thanks for the book reference! I opened it up - and it seems to answer nearly every question I've had! (even the ones that took hours to figure out by myself) Thanks!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom Control : One custom property to array of controls anon ASP .Net Web Controls 0 01-15-2005 11:32 AM
Using Summary Validation control with server Custom validation =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?= ASP .Net 2 10-15-2004 06:15 PM
Custom Server Control Property Validation Issue Jim-M ASP .Net Building Controls 3 05-12-2004 11:11 PM
Keep custom property-value in custom rendered control Rob Joosen ASP .Net Building Controls 0 05-12-2004 11:56 AM
Re: only custom validation control does server side validation? Colin Mackay ASP .Net 0 06-25-2003 07:54 AM



Advertisments