Default Values for Control Properties

Discussion in 'ASP .Net Building Controls' started by dolfandon, May 3, 2006.

  1. dolfandon

    dolfandon Guest

    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, May 3, 2006
    #1
    1. Advertisements

  2. dolfandon

    Brock Allen Guest

    Use reflection:

    object[] attrs = typeof(YourClass).GetProperty("YourProperty").GetCustomAttributes(typeof(DefaultPropertyAttribute),
    false);
    DefaultPropertyAttribute attr = attrs[0] as DefaultPropertyAttribute;
    object value = attr.Value;
     
    Brock Allen, May 3, 2006
    #2
    1. Advertisements

  3. dolfandon

    MSDN Guest

    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


     
    MSDN, May 4, 2006
    #3
  4. dolfandon

    MasterGaurav Guest

    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;
    }
    }
     
    MasterGaurav, May 4, 2006
    #4
  5. dolfandon

    dolfandon Guest

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

    [DefaultValue("defaultForProperty1")]
    public string Property1
    {
    get
    {
    return (ValueNotInViewState() ? "defaultForProperty1" :
    ValueInViewState);
    }
    set
    {
    SaveValueInViewState(value);
    }
    }
     
    dolfandon, May 4, 2006
    #5
  6. dolfandon

    Brock Allen Guest

    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").GetCustomAttributes(typeof(DefaultValueAttribute),
    false);
    DefaultValueAttribute attr = attrs[0] as DefaultValueAttribute;
    object val = attr.Value;
    Console.WriteLine(val);
    }
    }




     
    Brock Allen, May 4, 2006
    #6
  7. dolfandon

    MSDN Guest

    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


     
    MSDN, May 4, 2006
    #7
  8. dolfandon

    Brock Allen Guest

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



     
    Brock Allen, May 4, 2006
    #8
  9. dolfandon

    MSDN Guest

    Used your exact code, still getting 0 (zero)

    double wid = TextBox1.Width.Value;
    object[] attrs =typeof(System.Web.UI.WebControls.TextBox).GetProperty("Width").GetCustomAttributes(typeof(System.ComponentModel.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

     
    MSDN, May 4, 2006
    #9
  10. dolfandon

    Brock Allen Guest

    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 Allen, May 4, 2006
    #10
  11. dolfandon

    MSDN Guest

    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


     
    MSDN, May 4, 2006
    #11
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.