Default Values for Control Properties

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Default Values for Control Properties

    I have a custom control that has a bunch of properties on it. I added
    the DefaultValue attribute to it and it all seems to work fine with
    showing up in the desiger and all. My question is, how do I get at that
    default value at run time? If the user does not enter anything in the
    designer then the entry never gets put in the control tag in the aspx
    page and when I read the value it is empty. What is the magic to get
    the default value or do I have to put it in the get of the property but
    that seems clunky.

    ASP.Net 2.0 for the record.

    TIA
    Don

    dolfandon@gmail.com Guest

  2. Similar Questions and Discussions

    1. How do I validate the values entered for custom control properties?
      I have a custom control with properties named MinValue, MaxValue, and Value (all of which I have assigned a DefaultValue design-time attribute). The...
    2. CreateChildControls() always has default values for properties
      I am trying to develop a composite control that renders in a specific layout, depending on the value of the custom LabelPosition property. The...
    3. Specifying dynamic default values for properties in custom controls:
      Hey guys, I have a custom int property in my control whose default value is dynamic, so I cannot specify a "DefaultValue" attribute. However, I...
    4. customizing attribute values for control properties
      Is there somewhere an example on how to customize the serialization of control properties to aspx/ascx control attributes? I've implemented...
    5. how to set default values for non-basic type properties?
      "Ben Schwehn" <b.schwehn@gmx.net> wrote in message news:pan.2003.09.21.20.08.15.169997@gmx.net... When you add a DefaultValue attribute to a...
  3. #2

    Default Re: Default Values for Control Properties

    Use reflection:

    object[] attrs = typeof(YourClass).GetProperty("YourProperty").GetC ustomAttributes(typeof(DefaultPropertyAttribute),
    false);
    DefaultPropertyAttribute attr = attrs[0] as DefaultPropertyAttribute;
    object value = attr.Value;

    -Brock
    [url]http://staff.develop.com/ballen[/url]

    > I have a custom control that has a bunch of properties on it. I added
    > the DefaultValue attribute to it and it all seems to work fine with
    > showing up in the desiger and all. My question is, how do I get at
    > that default value at run time? If the user does not enter anything in
    > the designer then the entry never gets put in the control tag in the
    > aspx page and when I read the value it is empty. What is the magic to
    > get the default value or do I have to put it in the get of the
    > property but that seems clunky.
    >
    > ASP.Net 2.0 for the record.
    >
    > TIA Don
    >

    Brock Allen Guest

  4. #3

    Default Re: Default Values for Control Properties

    Brock,

    I can't make your reflection example to work.
    For example in a class called "Foo", a TextBox1 and to get the Width
    What do you put , and is this good for FW 2.0?

    Thanks


    "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    news:b8743b1b87d8c83ccdb7b4c296@msnews.microsoft.c om...
    > Use reflection:
    >
    > object[] attrs =
    > typeof(YourClass).GetProperty("YourProperty").GetC ustomAttributes(typeof(DefaultPropertyAttribute),
    > false);
    > DefaultPropertyAttribute attr = attrs[0] as DefaultPropertyAttribute;
    > object value = attr.Value;
    >
    > -Brock
    > [url]http://staff.develop.com/ballen[/url]
    >
    >
    >> I have a custom control that has a bunch of properties on it. I added
    >> the DefaultValue attribute to it and it all seems to work fine with
    >> showing up in the desiger and all. My question is, how do I get at
    >> that default value at run time? If the user does not enter anything in
    >> the designer then the entry never gets put in the control tag in the
    >> aspx page and when I read the value it is empty. What is the magic to
    >> get the default value or do I have to put it in the get of the
    >> property but that seems clunky.
    >>
    >> ASP.Net 2.0 for the record.
    >>
    >> TIA Don
    >>
    >
    >

    MSDN Guest

  5. #4

    Default Re: Default Values for Control Properties

    Adding to Brock's comments...

    Default Value attribute is only for the designer.
    Runtime has nothing to do with it... it never makes use of it.
    It if your responsibility to provide the value.

    btw, where are you writing the the code that Brock provided?

    Simplest way:

    public PropertyType PropertyName
    {
    get
    {
    object obj = GetFromViewState...
    if(obj != null)
    {
    (PropertyType) obj;
    }
    return DefaultValue_Of_PropertyName;
    }
    }


    --
    Cheers,
    Gaurav Vaish
    [url]http://mastergaurav.org[/url]
    [url]http://www.edujini.in[/url]
    -------------------------------
    To Hell with Murphy's Law.. When things go broke, FIX Them
    -------------------------------

    MasterGaurav Guest

  6. #5

    Default Re: Default Values for Control Properties

    I ended up doing what Gaurav has (sort of) :)

    [DefaultValue("defaultForProperty1")]
    public string Property1
    {
    get
    {
    return (ValueNotInViewState() ? "defaultForProperty1" :
    ValueInViewState);
    }
    set
    {
    SaveValueInViewState(value);
    }
    }

    dolfandon@gmail.com Guest

  7. #6

    Default Re: Default Values for Control Properties

    This works for me:

    using System;
    using System.ComponentModel;

    public class Foo
    {
    [DefaultValue(5)]
    public int Data
    {
    get
    {
    return 5;
    }
    }
    }

    class App
    {
    static void Main(string[] args)
    {
    object[] attrs =
    typeof(Foo).GetProperty("Data").GetCustomAttribute s(typeof(DefaultValueAttribute),
    false);
    DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
    object val = attr.Value;
    Console.WriteLine(val);
    }
    }


    -Brock
    [url]http://staff.develop.com/ballen[/url]

    > Brock,
    >
    > I can't make your reflection example to work.
    > For example in a class called "Foo", a TextBox1 and to get the Width
    > What do you put , and is this good for FW 2.0?
    > Thanks
    >
    > "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    > news:b8743b1b87d8c83ccdb7b4c296@msnews.microsoft.c om...
    >
    >> Use reflection:
    >>
    >> object[] attrs =
    >> typeof(YourClass).GetProperty("YourProperty").GetC ustomAttributes(typ
    >> eof(DefaultPropertyAttribute),
    >> false);
    >> DefaultPropertyAttribute attr = attrs[0] as DefaultPropertyAttribute;
    >> object value = attr.Value;
    >> -Brock
    >> [url]http://staff.develop.com/ballen[/url]
    >>> I have a custom control that has a bunch of properties on it. I
    >>> added the DefaultValue attribute to it and it all seems to work fine
    >>> with showing up in the desiger and all. My question is, how do I get
    >>> at that default value at run time? If the user does not enter
    >>> anything in the designer then the entry never gets put in the
    >>> control tag in the aspx page and when I read the value it is empty.
    >>> What is the magic to get the default value or do I have to put it in
    >>> the get of the property but that seems clunky.
    >>>
    >>> ASP.Net 2.0 for the record.
    >>>
    >>> TIA Don
    >>>

    Brock Allen Guest

  8. #7

    Default Re: Default Values for Control Properties

    Brock,

    First Thank you,
    Second: When I drop a TextBox on a web form it has some default properties
    like width and height.
    Without assigning any values to the TextBox above, How do I get these
    defaults?

    Thank You,

    SA


    "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    news:b8743b1bb928c83d9c32474ee2@msnews.microsoft.c om...
    > This works for me:
    >
    > using System;
    > using System.ComponentModel;
    >
    > public class Foo
    > {
    > [DefaultValue(5)]
    > public int Data
    > {
    > get
    > {
    > return 5;
    > }
    > }
    > }
    >
    > class App
    > {
    > static void Main(string[] args)
    > {
    > object[] attrs =
    > typeof(Foo).GetProperty("Data").GetCustomAttribute s(typeof(DefaultValueAttribute),
    > false);
    > DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
    > object val = attr.Value;
    > Console.WriteLine(val);
    > }
    > }
    >
    >
    > -Brock
    > [url]http://staff.develop.com/ballen[/url]
    >
    >
    >> Brock,
    >>
    >> I can't make your reflection example to work.
    >> For example in a class called "Foo", a TextBox1 and to get the Width
    >> What do you put , and is this good for FW 2.0?
    >> Thanks
    >>
    >> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    >> news:b8743b1b87d8c83ccdb7b4c296@msnews.microsoft.c om...
    >>
    >>> Use reflection:
    >>>
    >>> object[] attrs =
    >>> typeof(YourClass).GetProperty("YourProperty").GetC ustomAttributes(typ
    >>> eof(DefaultPropertyAttribute),
    >>> false);
    >>> DefaultPropertyAttribute attr = attrs[0] as DefaultPropertyAttribute;
    >>> object value = attr.Value;
    >>> -Brock
    >>> [url]http://staff.develop.com/ballen[/url]
    >>>> I have a custom control that has a bunch of properties on it. I
    >>>> added the DefaultValue attribute to it and it all seems to work fine
    >>>> with showing up in the desiger and all. My question is, how do I get
    >>>> at that default value at run time? If the user does not enter
    >>>> anything in the designer then the entry never gets put in the
    >>>> control tag in the aspx page and when I read the value it is empty.
    >>>> What is the magic to get the default value or do I have to put it in
    >>>> the get of the property but that seems clunky.
    >>>>
    >>>> ASP.Net 2.0 for the record.
    >>>>
    >>>> TIA Don
    >>>>
    >
    >

    MSDN Guest

  9. #8

    Default Re: Default Values for Control Properties

    In my sample code change "Foo" to "TextBox" and change "Data" to "Width".

    -Brock
    [url]http://staff.develop.com/ballen[/url]

    > Brock,
    >
    > First Thank you,
    > Second: When I drop a TextBox on a web form it has some default
    > properties
    > like width and height.
    > Without assigning any values to the TextBox above, How do I get these
    > defaults?
    > Thank You,
    >
    > SA
    >
    > "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    > news:b8743b1bb928c83d9c32474ee2@msnews.microsoft.c om...
    >
    >> This works for me:
    >>
    >> using System;
    >> using System.ComponentModel;
    >> public class Foo
    >> {
    >> [DefaultValue(5)]
    >> public int Data
    >> {
    >> get
    >> {
    >> return 5;
    >> }
    >> }
    >> }
    >> class App
    >> {
    >> static void Main(string[] args)
    >> {
    >> object[] attrs =
    >> typeof(Foo).GetProperty("Data").GetCustomAttribute s(typeof(DefaultVal
    >> ueAttribute),
    >> false);
    >> DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
    >> object val = attr.Value;
    >> Console.WriteLine(val);
    >> }
    >> }
    >> -Brock
    >> [url]http://staff.develop.com/ballen[/url]
    >>> Brock,
    >>>
    >>> I can't make your reflection example to work.
    >>> For example in a class called "Foo", a TextBox1 and to get the Width
    >>> What do you put , and is this good for FW 2.0?
    >>> Thanks
    >>> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    >>> news:b8743b1b87d8c83ccdb7b4c296@msnews.microsoft.c om...
    >>>
    >>>> Use reflection:
    >>>>
    >>>> object[] attrs =
    >>>> typeof(YourClass).GetProperty("YourProperty").GetC ustomAttributes(t
    >>>> yp
    >>>> eof(DefaultPropertyAttribute),
    >>>> false);
    >>>> DefaultPropertyAttribute attr = attrs[0] as
    >>>> DefaultPropertyAttribute;
    >>>> object value = attr.Value;
    >>>> -Brock
    >>>> [url]http://staff.develop.com/ballen[/url]
    >>>>> I have a custom control that has a bunch of properties on it. I
    >>>>> added the DefaultValue attribute to it and it all seems to work
    >>>>> fine with showing up in the desiger and all. My question is, how
    >>>>> do I get at that default value at run time? If the user does not
    >>>>> enter anything in the designer then the entry never gets put in
    >>>>> the control tag in the aspx page and when I read the value it is
    >>>>> empty. What is the magic to get the default value or do I have to
    >>>>> put it in the get of the property but that seems clunky.
    >>>>>
    >>>>> ASP.Net 2.0 for the record.
    >>>>>
    >>>>> TIA Don
    >>>>>

    Brock Allen Guest

  10. #9

    Default Re: Default Values for Control Properties

    Used your exact code, still getting 0 (zero)

    double wid = TextBox1.Width.Value;
    object[] attrs =typeof(System.Web.UI.WebControls.TextBox).GetProp erty("Width").GetCustomAttributes(typeof(System.Co mponentModel.DefaultValueAttribute),false);
    DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
    object val = attr.Value;

    I guess this info is not available for web controls???????????????????????

    Thank you Brock
    SA

    "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message news:b8743b1bbbb8c83daaa0da946e@msnews.microsoft.c om...
    > In my sample code change "Foo" to "TextBox" and change "Data" to "Width".
    >
    > -Brock
    > [url]http://staff.develop.com/ballen[/url]
    >
    >
    >> Brock,
    >>
    >> First Thank you,
    >> Second: When I drop a TextBox on a web form it has some default
    >> properties
    >> like width and height.
    >> Without assigning any values to the TextBox above, How do I get these
    >> defaults?
    >> Thank You,
    >>
    >> SA
    >>
    >> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    >> news:b8743b1bb928c83d9c32474ee2@msnews.microsoft.c om...
    >>
    >>> This works for me:
    >>>
    >>> using System;
    >>> using System.ComponentModel;
    >>> public class Foo
    >>> {
    >>> [DefaultValue(5)]
    >>> public int Data
    >>> {
    >>> get
    >>> {
    >>> return 5;
    >>> }
    >>> }
    >>> }
    >>> class App
    >>> {
    >>> static void Main(string[] args)
    >>> {
    >>> object[] attrs =
    >>> typeof(Foo).GetProperty("Data").GetCustomAttribute s(typeof(DefaultVal
    >>> ueAttribute),
    >>> false);
    >>> DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
    >>> object val = attr.Value;
    >>> Console.WriteLine(val);
    >>> }
    >>> }
    >>> -Brock
    >>> [url]http://staff.develop.com/ballen[/url]
    >>>> Brock,
    >>>>
    >>>> I can't make your reflection example to work.
    >>>> For example in a class called "Foo", a TextBox1 and to get the Width
    >>>> What do you put , and is this good for FW 2.0?
    >>>> Thanks
    >>>> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    >>>> news:b8743b1b87d8c83ccdb7b4c296@msnews.microsoft.c om...
    >>>>
    >>>>> Use reflection:
    >>>>>
    >>>>> object[] attrs =
    >>>>> typeof(YourClass).GetProperty("YourProperty").GetC ustomAttributes(t
    >>>>> yp
    >>>>> eof(DefaultPropertyAttribute),
    >>>>> false);
    >>>>> DefaultPropertyAttribute attr = attrs[0] as
    >>>>> DefaultPropertyAttribute;
    >>>>> object value = attr.Value;
    >>>>> -Brock
    >>>>> [url]http://staff.develop.com/ballen[/url]
    >>>>>> I have a custom control that has a bunch of properties on it. I
    >>>>>> added the DefaultValue attribute to it and it all seems to work
    >>>>>> fine with showing up in the desiger and all. My question is, how
    >>>>>> do I get at that default value at run time? If the user does not
    >>>>>> enter anything in the designer then the entry never gets put in
    >>>>>> the control tag in the aspx page and when I read the value it is
    >>>>>> empty. What is the magic to get the default value or do I have to
    >>>>>> put it in the get of the property but that seems clunky.
    >>>>>>
    >>>>>> ASP.Net 2.0 for the record.
    >>>>>>
    >>>>>> TIA Don
    >>>>>>
    >
    >
    MSDN Guest

  11. #10

    Default Re: Default Values for Control Properties

    Does the array have zero or one element in it? If it has one element, then
    that's telling you the DefaultValue attribute has been appliied to the Width
    proeprty and the default value is "" (the empty string). Looking with reflector,
    that's what I'm expecting:

    [WebCategory("Layout"), DefaultValue(typeof(Unit), ""), WebSysDescription("WebControl_Width")]
    public virtual Unit Width
    {
    get
    {
    if (!this.ControlStyleCreated)
    {
    return Unit.Empty;
    }
    return this.ControlStyle.Width;
    }
    set
    {
    this.ControlStyle.Width = value;
    }
    }


    -Brock
    [url]http://staff.develop.com/ballen[/url]

    > Used your exact code, still getting 0 (zero)
    >
    > double wid = TextBox1.Width.Value;
    > object[] attrs
    > =typeof(System.Web.UI.WebControls.TextBox).GetProp erty("Width").GetCus
    > t
    > omAttributes(typeof(System.ComponentModel.DefaultV alueAttribute),false
    > );
    > DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
    > object val = attr.Value;
    > I guess this info is not available for web
    > controls???????????????????????
    >
    > Thank you Brock
    > SA
    > "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    > news:b8743b1bbbb8c83daaa0da946e@msnews.microsoft.c om...
    >
    >> In my sample code change "Foo" to "TextBox" and change "Data" to
    >>
    > "Width".
    >
    >> -Brock
    >> [url]http://staff.develop.com/ballen[/url]
    >>> Brock,
    >>>
    >>> First Thank you,
    >>> Second: When I drop a TextBox on a web form it has some default
    >>> properties
    >>> like width and height.
    >>> Without assigning any values to the TextBox above, How do I get
    >>> these
    >>> defaults?
    >>> Thank You,
    >>> SA
    >>>
    >>> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    >>> news:b8743b1bb928c83d9c32474ee2@msnews.microsoft.c om...
    >>>
    >>>> This works for me:
    >>>>
    >>>> using System;
    >>>> using System.ComponentModel;
    >>>> public class Foo
    >>>> {
    >>>> [DefaultValue(5)]
    >>>> public int Data
    >>>> {
    >>>> get
    >>>> {
    >>>> return 5;
    >>>> }
    >>>> }
    >>>> }
    >>>> class App
    >>>> {
    >>>> static void Main(string[] args)
    >>>> {
    >>>> object[] attrs =
    > typeof(Foo).GetProperty("Data").GetCustomAttribute s(typeof(DefaultVal
    >
    >>>> ueAttribute),
    >>>> false);
    >>>> DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
    >>>> object val = attr.Value;
    >>>> Console.WriteLine(val);
    >>>> }
    >>>> }
    >>>> -Brock
    >>>> [url]http://staff.develop.com/ballen[/url]
    >>>>> Brock,
    >>>>>
    >>>>> I can't make your reflection example to work.
    >>>>> For example in a class called "Foo", a TextBox1 and to get the
    > Width
    >
    >>>>> What do you put , and is this good for FW 2.0?
    >>>>> Thanks
    >>>>> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    >>>>> news:b8743b1b87d8c83ccdb7b4c296@msnews.microsoft.c om...
    >>>>>> Use reflection:
    >>>>>>
    >>>>>> object[] attrs =
    >>>>>>
    > typeof(YourClass).GetProperty("YourProperty").GetC ustomAttributes(t
    >
    >>>>>> yp
    >>>>>> eof(DefaultPropertyAttribute),
    >>>>>> false);
    >>>>>> DefaultPropertyAttribute attr = attrs[0] as
    >>>>>> DefaultPropertyAttribute;
    >>>>>> object value = attr.Value;
    >>>>>> -Brock
    >>>>>> [url]http://staff.develop.com/ballen[/url]
    >>>>>>> I have a custom control that has a bunch of properties on it. I
    >>>>>>> added the DefaultValue attribute to it and it all seems to work
    >>>>>>> fine with showing up in the desiger and all. My question is, how
    >>>>>>> do I get at that default value at run time? If the user does not
    >>>>>>> enter anything in the designer then the entry never gets put in
    >>>>>>> the control tag in the aspx page and when I read the value it is
    >>>>>>> empty. What is the magic to get the default value or do I have
    >>>>>>> to put it in the get of the property but that seems clunky.
    >>>>>>>
    >>>>>>> ASP.Net 2.0 for the record.
    >>>>>>>
    >>>>>>> TIA Don
    >>>>>>>

    Brock Allen Guest

  12. #11

    Default Re: Default Values for Control Properties

    Brock,

    We might be thinking about different things here. may be not?
    when you drop a TextBox control on the Designer it has a default width,
    height etc...
    How do you get that default width.
    Microsoft must be getting this default from some place ( meta data of
    defaults etc.. )
    I was hoping that your code can tell me how to get this default value.
    Microsoft must be baking this someplace.

    If the default is "" String.Empty(); then how does it show at design and run
    time at some width greater than 50px ++ ; ??

    Are we on the right page?

    Thank you Brock

    SA


    "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    news:b8743b1bc0d8c83db42c39a49c@msnews.microsoft.c om...
    > Does the array have zero or one element in it? If it has one element, then
    > that's telling you the DefaultValue attribute has been appliied to the
    > Width proeprty and the default value is "" (the empty string). Looking
    > with reflector, that's what I'm expecting:
    >
    > [WebCategory("Layout"), DefaultValue(typeof(Unit), ""),
    > WebSysDescription("WebControl_Width")]
    > public virtual Unit Width
    > {
    > get
    > {
    > if (!this.ControlStyleCreated)
    > {
    > return Unit.Empty;
    > }
    > return this.ControlStyle.Width;
    > }
    > set
    > {
    > this.ControlStyle.Width = value;
    > }
    > }
    >
    >
    > -Brock
    > [url]http://staff.develop.com/ballen[/url]
    >
    >
    >> Used your exact code, still getting 0 (zero)
    >>
    >> double wid = TextBox1.Width.Value;
    >> object[] attrs
    >> =typeof(System.Web.UI.WebControls.TextBox).GetProp erty("Width").GetCus
    >> t
    >> omAttributes(typeof(System.ComponentModel.DefaultV alueAttribute),false
    >> );
    >> DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
    >> object val = attr.Value;
    >> I guess this info is not available for web
    >> controls???????????????????????
    >>
    >> Thank you Brock
    >> SA
    >> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    >> news:b8743b1bbbb8c83daaa0da946e@msnews.microsoft.c om...
    >>
    >>> In my sample code change "Foo" to "TextBox" and change "Data" to
    >>>
    >> "Width".
    >>
    >>> -Brock
    >>> [url]http://staff.develop.com/ballen[/url]
    >>>> Brock,
    >>>>
    >>>> First Thank you,
    >>>> Second: When I drop a TextBox on a web form it has some default
    >>>> properties
    >>>> like width and height.
    >>>> Without assigning any values to the TextBox above, How do I get
    >>>> these
    >>>> defaults?
    >>>> Thank You,
    >>>> SA
    >>>>
    >>>> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    >>>> news:b8743b1bb928c83d9c32474ee2@msnews.microsoft.c om...
    >>>>
    >>>>> This works for me:
    >>>>>
    >>>>> using System;
    >>>>> using System.ComponentModel;
    >>>>> public class Foo
    >>>>> {
    >>>>> [DefaultValue(5)]
    >>>>> public int Data
    >>>>> {
    >>>>> get
    >>>>> {
    >>>>> return 5;
    >>>>> }
    >>>>> }
    >>>>> }
    >>>>> class App
    >>>>> {
    >>>>> static void Main(string[] args)
    >>>>> {
    >>>>> object[] attrs =
    >> typeof(Foo).GetProperty("Data").GetCustomAttribute s(typeof(DefaultVal
    >>
    >>>>> ueAttribute),
    >>>>> false);
    >>>>> DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
    >>>>> object val = attr.Value;
    >>>>> Console.WriteLine(val);
    >>>>> }
    >>>>> }
    >>>>> -Brock
    >>>>> [url]http://staff.develop.com/ballen[/url]
    >>>>>> Brock,
    >>>>>>
    >>>>>> I can't make your reflection example to work.
    >>>>>> For example in a class called "Foo", a TextBox1 and to get the
    >> Width
    >>
    >>>>>> What do you put , and is this good for FW 2.0?
    >>>>>> Thanks
    >>>>>> "Brock Allen" <ballen@NOSPAMdevelop.com> wrote in message
    >>>>>> news:b8743b1b87d8c83ccdb7b4c296@msnews.microsoft.c om...
    >>>>>>> Use reflection:
    >>>>>>>
    >>>>>>> object[] attrs =
    >>>>>>>
    >> typeof(YourClass).GetProperty("YourProperty").GetC ustomAttributes(t
    >>
    >>>>>>> yp
    >>>>>>> eof(DefaultPropertyAttribute),
    >>>>>>> false);
    >>>>>>> DefaultPropertyAttribute attr = attrs[0] as
    >>>>>>> DefaultPropertyAttribute;
    >>>>>>> object value = attr.Value;
    >>>>>>> -Brock
    >>>>>>> [url]http://staff.develop.com/ballen[/url]
    >>>>>>>> I have a custom control that has a bunch of properties on it. I
    >>>>>>>> added the DefaultValue attribute to it and it all seems to work
    >>>>>>>> fine with showing up in the desiger and all. My question is, how
    >>>>>>>> do I get at that default value at run time? If the user does not
    >>>>>>>> enter anything in the designer then the entry never gets put in
    >>>>>>>> the control tag in the aspx page and when I read the value it is
    >>>>>>>> empty. What is the magic to get the default value or do I have
    >>>>>>>> to put it in the get of the property but that seems clunky.
    >>>>>>>>
    >>>>>>>> ASP.Net 2.0 for the record.
    >>>>>>>>
    >>>>>>>> TIA Don
    >>>>>>>>
    >
    >

    MSDN Guest

  13. #12

    Default Re: Default Values for Control Properties

    >I ended up doing what Gaurav has (sort of) :)


    :-)

    --
    Happy Hacking,
    Gaurav Vaish
    [url]http://www.mastergaurav.org[/url]
    [url]http://www.edujini.in[/url]
    -------------------
    >
    > [DefaultValue("defaultForProperty1")]
    > public string Property1
    > {
    > get
    > {
    > return (ValueNotInViewState() ? "defaultForProperty1" :
    > ValueInViewState);
    > }
    > set
    > {
    > SaveValueInViewState(value);
    > }
    > }
    >

    Gaurav Vaish \(EduJini.IN\) Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139