Custom Server Control Property Validation Issue

Discussion in 'ASP .Net Web Controls' started by Jim-M, May 11, 2004.

  1. Jim-M

    Jim-M Guest

    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 ---
     
    Jim-M, May 11, 2004
    #1
    1. Advertising

  2. Jim-M

    Jim-M Guest

    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]" <> wrote in message
    news:...
    > Jim, just some things off the top of my head to try... try adding the
    > following attributes to both properties:
    >
    > PersistenceMode(PersistenceMode.Attribute),
    >

    DesignerSerializationVisibility(DesignerSerializationVisibility.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.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
     
    Jim-M, May 12, 2004
    #2
    1. Advertising

  3. Jim-M

    Jim-M Guest

    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]" <> wrote in message
    news:...
    > Jim, just some things off the top of my head to try... try adding the
    > following attributes to both properties:
    >
    > PersistenceMode(PersistenceMode.Attribute),
    >

    DesignerSerializationVisibility(DesignerSerializationVisibility.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.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
     
    Jim-M, May 12, 2004
    #3
  4. 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/ASIN/0735615829/4guysfromrollaco

    Happy Programming!

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], May 13, 2004
    #4
  5. 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!
     
    William Mansfield, May 13, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Mackay
    Replies:
    0
    Views:
    2,695
    Colin Mackay
    Jun 25, 2003
  2. =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?=

    Using Summary Validation control with server Custom validation

    =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?=, Oct 15, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    884
    =?Utf-8?B?QmFyYmFyYSBBbGRlcnRvbg==?=
    Oct 15, 2004
  3. Jim-M

    Custom Server Control Property Validation Issue

    Jim-M, May 11, 2004, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    146
    Scott Mitchell [MVP]
    May 13, 2004
  4. Rob Joosen

    Keep custom property-value in custom rendered control

    Rob Joosen, May 12, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    147
    Rob Joosen
    May 12, 2004
  5. anon
    Replies:
    0
    Views:
    212
Loading...

Share This Page