Home All Groups Group Topic Archive Search About
Author
9 May 2005 4:51 PM
STech
I would like to add *designtime* and *runtime* checks to two properties in a
custom webcontrol
1) I would like to check the value of the property (int) and make sure it is
in a certain range.
2) I would like to compare two properties on the control anytime one
property is changed and make sure one is greater than the other.

The properties are similar to the minvalue, maxvalue properties of the range
validator.

What is the recommended way of doing this? I could add the check during the
"get" of a property but that does not work in design time.

Thanks.

Author
10 May 2005 6:01 AM
Steven Cheng[MSFT]
Hi Stech,

Welcome to ASPNET newsgroup.
As for the validate value for properties of ASP.NET Custome WebControl,
here are some of my suggestions;

Generally for intercepting  control's property accessing, we have two means:

1. Using the Property's "Set" accessor, this is very convenient which can
meet simple requirement of validating. We can just put our validation code
in  the property's setter accssor like:

===================
private int _min;
private int _max;

.....

public int Min {....}

public int Max
{
    get
    {
        return _max;
    }
    set
    {
        if(value < _min)
        {
            if(this.Site != null)
            {
                MessageBox.Show("Max value can't be less than Min value!");
            }
            else
            {
                throw new Exception("Max value can't be less than Min value!");
            }

            return;
        }
        _max = value;
    }
}
=====================

the

if(this.Site != null)

statement is used to determine whether it is at designtime or runtime, at
design-time, the Control.Site is null( we can also use HttpContext.Current
!= null to make the same determination).

The drawbacks of this approach is that we will mix the design-time
validation code with the runtime code. but this is really a very simple
means.

2. Use a  custom TypeConverter and apply it to our control proprety, in
TypeConverter, we can do complex valdation on property's value. However,
when we need to validate property value depending on other properties( of
the same control), it's better to directly put the validating code in the
property's  "set" accsssor.

#TypeConverter Class
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemComponentModel
TypeConverterClassTopic.asp?frame=true

Hope helps. Thanks,


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
10 May 2005 3:51 PM
STech
Steven,

Why do you need this (MessageBox.Show)?

if(this.Site != null)
{
MessageBox.Show("Max value can't be less than Min value!");
}
else
{
throw new Exception("Max value can't be less than Min value!");
}

Why not only use the throw new Exception. This works in both design time and
run time . ie use only this.

if(value < _min)
{
throw new Exception("Max value can't be less than Min value!");
return;
}




Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> Hi Stech,
>
> Welcome to ASPNET newsgroup.
> As for the validate value for properties of ASP.NET Custome WebControl,
> here are some of my suggestions;
>
> Generally for intercepting  control's property accessing, we have two means:
>
> 1. Using the Property's "Set" accessor, this is very convenient which can
> meet simple requirement of validating. We can just put our validation code
> in  the property's setter accssor like:
>
> ===================
> private int _min;
> private int _max;
>
> .....
>
> public int Min {....}
>
> public int Max
> {
>     get
>     {
>         return _max;
>     }
>     set
>     {
>         if(value < _min)
>         {
>             if(this.Site != null)
>             {
>                 MessageBox.Show("Max value can't be less than Min value!");
>             }
>             else
>             {
>                 throw new Exception("Max value can't be less than Min value!");
>             }
>
>             return;
>         }
>         _max = value;
>     }
> }
> =====================
>
> the
>
> if(this.Site != null)
>
> statement is used to determine whether it is at designtime or runtime, at
> design-time, the Control.Site is null( we can also use HttpContext.Current
> != null to make the same determination).
>
> The drawbacks of this approach is that we will mix the design-time
> validation code with the runtime code. but this is really a very simple
> means.
>
> 2. Use a  custom TypeConverter and apply it to our control proprety, in
> TypeConverter, we can do complex valdation on property's value. However,
> when we need to validate property value depending on other properties( of
> the same control), it's better to directly put the validating code in the
> property's  "set" accsssor.
>
> #TypeConverter Class
> http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemComponentModel
> TypeConverterClassTopic.asp?frame=true
>
> Hope helps. Thanks,
>
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
>
>
>
>
>
>
>
>
>
Author
11 May 2005 12:41 AM
Steven Cheng[MSFT]
Thanks for your reply Stech,

The

"MessageBox.Show" I used just indicate that we can show winform messagebox
at design-time( but can't at runtime). Also, the

if....else...

statement just used to separate the error processing code at
design-time(show messagebox)  from the code at runtime
(at runtime).

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Author
11 May 2005 5:09 AM
STech
Steven,
Sorry for not being clear. I do not understand why you have the
messagebox.show method..

The following works in both design time and run time. Is there anything else
I need to be aware of?

public int Max
{
    get
    {
        return _max;
    }
    set
    {
        if(value < _min)
        {
                throw new Exception("Max value can't be less than Min value!");
            return;
        }
        _max = value;
    }
}


Show quoteHide quote
"Steven Cheng[MSFT]" wrote:

> Thanks for your reply Stech,
>
> The
>
> "MessageBox.Show" I used just indicate that we can show winform messagebox
> at design-time( but can't at runtime). Also, the
>
> if....else...
>
> statement just used to separate the error processing code at
> design-time(show messagebox)  from the code at runtime
> (at runtime).
>
> Thanks,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
Author
12 May 2005 5:49 AM
Steven Cheng[MSFT]
Hi Stech,

Maybe we've just misunderstood each other.   Yes, your code are quite well.
No "MessageBox"  needed , that's just my own test code :-).  

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)