DefaultValue is not initialized

Discussion in 'ASP .Net Web Controls' started by Oleg Slyusarchuk, May 18, 2004.

  1. This one should be a very simple one...

    in a I Control (System.Web.UI.Control) have a property DefaultErrorContent
    defined as:

    // member variable
    private string _errorContent;


    [Browsable(true),
    DefaultValue("There is a small problem. Don't worry."),
    Description("Default content that will be shown at run-time when all the
    recovery efforts fail")]
    public string DefaultErrorContent
    {
    get { return _errorContent; }
    set
    {
    _errorContent = value;
    }
    }

    I can set this property at design time and everything works fine, saving
    changed property inside of control's attribute.

    BUT,

    when I don't touch the value, I'd expect it to be set up to the
    defaultValue, and this does not happen.
    The errorContent == null when I check it.

    What do I do wrong?

    Thanks in advance
     
    Oleg Slyusarchuk, May 18, 2004
    #1
    1. Advertising

  2. Oleg Slyusarchuk wrote:
    > when I don't touch the value, I'd expect it to be set up to the
    > defaultValue, and this does not happen.
    > The errorContent == null when I check it.
    >
    > What do I do wrong?


    Do:

    // member variable
    private string _errorContent = **DEFAULT VALUE**;

    See, the DefaultValue attribute just tells VS.NET what default attribute
    to show in the Designer. It does not cause that default value to be
    assigned to the property when the control is actually instantiated in
    the page's control hierarchy.

    Actually, your property should be storing the value of the property to
    the ViewState StateBag, so that this value can be changed
    programmatically...

    hth

    --

    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 19, 2004
    #2
    1. Advertising

  3. Thanks,

    Finally I realized that this to be used by Designer mainly, and there is
    almost no use
    of DefaultValue in web controls since "The Web Forms designer does not
    support the ShouldSerializePropertyName and ResetPropertyName methods that
    are optionally exposed by Windows Forms controls for property persistence. "

    Thanks, your solution definitely is going to work.

    Oleg


    "Scott Mitchell [MVP]" <> wrote in message
    news:...
    > Oleg Slyusarchuk wrote:
    > > when I don't touch the value, I'd expect it to be set up to the
    > > defaultValue, and this does not happen.
    > > The errorContent == null when I check it.
    > >
    > > What do I do wrong?

    >
    > Do:
    >
    > // member variable
    > private string _errorContent = **DEFAULT VALUE**;
    >
    > See, the DefaultValue attribute just tells VS.NET what default attribute
    > to show in the Designer. It does not cause that default value to be
    > assigned to the property when the control is actually instantiated in
    > the page's control hierarchy.
    >
    > Actually, your property should be storing the value of the property to
    > the ViewState StateBag, so that this value can be changed
    > programmatically...
    >
    > hth
    >
    > --
    >
    > Scott Mitchell
    >
    > http://www.4GuysFromRolla.com
    > http://www.ASPFAQs.com
    > http://www.ASPMessageboard.com
    >
    > * When you think ASP, think 4GuysFromRolla.com!
     
    Oleg Slyusarchuk, May 19, 2004
    #3
  4. the thing is that when you set some property in the development environment
    some property walues are Bolded. These are those that have a different value
    from the Default Value. That's all this attribute does. So when you save
    your project environment only saves values that are different from the
    default. n your code you have to take care of the default value that it
    reflects the value set with DefaultValueAttribute class.

    Robert.

    "Oleg Slyusarchuk" <slyuso_AT_SOMETHING_LIKE_yahoo.com> wrote in message
    news:...
    > Thanks,
    >
    > Finally I realized that this to be used by Designer mainly, and there is
    > almost no use
    > of DefaultValue in web controls since "The Web Forms designer does not
    > support the ShouldSerializePropertyName and ResetPropertyName methods that
    > are optionally exposed by Windows Forms controls for property persistence.

    "
    >
    > Thanks, your solution definitely is going to work.
    >
    > Oleg
    >
    >
    > "Scott Mitchell [MVP]" <> wrote in message
    > news:...
    > > Oleg Slyusarchuk wrote:
    > > > when I don't touch the value, I'd expect it to be set up to the
    > > > defaultValue, and this does not happen.
    > > > The errorContent == null when I check it.
    > > >
    > > > What do I do wrong?

    > >
    > > Do:
    > >
    > > // member variable
    > > private string _errorContent = **DEFAULT VALUE**;
    > >
    > > See, the DefaultValue attribute just tells VS.NET what default attribute
    > > to show in the Designer. It does not cause that default value to be
    > > assigned to the property when the control is actually instantiated in
    > > the page's control hierarchy.
    > >
    > > Actually, your property should be storing the value of the property to
    > > the ViewState StateBag, so that this value can be changed
    > > programmatically...
    > >
    > > hth
    > >
    > > --
    > >
    > > Scott Mitchell
    > >
    > > http://www.4GuysFromRolla.com
    > > http://www.ASPFAQs.com
    > > http://www.ASPMessageboard.com
    > >
    > > * When you think ASP, think 4GuysFromRolla.com!

    >
    >
     
    Robert Koritnik, May 19, 2004
    #4
  5. Agree, absolutelly.

    However, I think, it'd be nice to initialize member variable (if it has not
    been intialized with something else in initialization section )
    with the value that is seen in Property Grid (the way it works when
    property's value is different from DefaultValue)

    Now, a developer has to take care with the synchronizing (well, setting them
    with the same initial value)
    both in property's DefaultValue and in initialization code.

    Actually, it's what I expected to be and what caused my question.

    Oleg


    "Robert Koritnik" <> wrote in message
    news:...
    > the thing is that when you set some property in the development

    environment
    > some property walues are Bolded. These are those that have a different

    value
    > from the Default Value. That's all this attribute does. So when you save
    > your project environment only saves values that are different from the
    > default. n your code you have to take care of the default value that it
    > reflects the value set with DefaultValueAttribute class.
    >
    > Robert.
    >
    > "Oleg Slyusarchuk" <slyuso_AT_SOMETHING_LIKE_yahoo.com> wrote in message
    > news:...
    > > Thanks,
    > >
    > > Finally I realized that this to be used by Designer mainly, and there is
    > > almost no use
    > > of DefaultValue in web controls since "The Web Forms designer does not
    > > support the ShouldSerializePropertyName and ResetPropertyName methods

    that
    > > are optionally exposed by Windows Forms controls for property

    persistence.
    > "
    > >
    > > Thanks, your solution definitely is going to work.
    > >
    > > Oleg
    > >
    > >
    > > "Scott Mitchell [MVP]" <> wrote in message
    > > news:...
    > > > Oleg Slyusarchuk wrote:
    > > > > when I don't touch the value, I'd expect it to be set up to the
    > > > > defaultValue, and this does not happen.
    > > > > The errorContent == null when I check it.
    > > > >
    > > > > What do I do wrong?
    > > >
    > > > Do:
    > > >
    > > > // member variable
    > > > private string _errorContent = **DEFAULT VALUE**;
    > > >
    > > > See, the DefaultValue attribute just tells VS.NET what default

    attribute
    > > > to show in the Designer. It does not cause that default value to be
    > > > assigned to the property when the control is actually instantiated in
    > > > the page's control hierarchy.
    > > >
    > > > Actually, your property should be storing the value of the property to
    > > > the ViewState StateBag, so that this value can be changed
    > > > programmatically...
    > > >
    > > > hth
    > > >
    > > > --
    > > >
    > > > Scott Mitchell
    > > >
    > > > http://www.4GuysFromRolla.com
    > > > http://www.ASPFAQs.com
    > > > http://www.ASPMessageboard.com
    > > >
    > > > * When you think ASP, think 4GuysFromRolla.com!

    > >
    > >

    >
    >
     
    Oleg Slyusarchuk, May 19, 2004
    #5
  6. You can still do that programaticly. You can check for the
    DefaultValueAttribute and do the appropriate thing.

    RobertK.

    "Oleg Slyusarchuk" <slyuso_AT_SOMETHING_LIKE_yahoo.com> wrote in message
    news:...
    > Agree, absolutelly.
    >
    > However, I think, it'd be nice to initialize member variable (if it has

    not
    > been intialized with something else in initialization section )
    > with the value that is seen in Property Grid (the way it works when
    > property's value is different from DefaultValue)
    >
    > Now, a developer has to take care with the synchronizing (well, setting

    them
    > with the same initial value)
    > both in property's DefaultValue and in initialization code.
    >
    > Actually, it's what I expected to be and what caused my question.
    >
    > Oleg
    >
    >
    > "Robert Koritnik" <> wrote in message
    > news:...
    > > the thing is that when you set some property in the development

    > environment
    > > some property walues are Bolded. These are those that have a different

    > value
    > > from the Default Value. That's all this attribute does. So when you save
    > > your project environment only saves values that are different from the
    > > default. n your code you have to take care of the default value that it
    > > reflects the value set with DefaultValueAttribute class.
    > >
    > > Robert.
    > >
    > > "Oleg Slyusarchuk" <slyuso_AT_SOMETHING_LIKE_yahoo.com> wrote in message
    > > news:...
    > > > Thanks,
    > > >
    > > > Finally I realized that this to be used by Designer mainly, and there

    is
    > > > almost no use
    > > > of DefaultValue in web controls since "The Web Forms designer does

    not
    > > > support the ShouldSerializePropertyName and ResetPropertyName methods

    > that
    > > > are optionally exposed by Windows Forms controls for property

    > persistence.
    > > "
    > > >
    > > > Thanks, your solution definitely is going to work.
    > > >
    > > > Oleg
    > > >
    > > >
    > > > "Scott Mitchell [MVP]" <> wrote in message
    > > > news:...
    > > > > Oleg Slyusarchuk wrote:
    > > > > > when I don't touch the value, I'd expect it to be set up to the
    > > > > > defaultValue, and this does not happen.
    > > > > > The errorContent == null when I check it.
    > > > > >
    > > > > > What do I do wrong?
    > > > >
    > > > > Do:
    > > > >
    > > > > // member variable
    > > > > private string _errorContent = **DEFAULT VALUE**;
    > > > >
    > > > > See, the DefaultValue attribute just tells VS.NET what default

    > attribute
    > > > > to show in the Designer. It does not cause that default value to be
    > > > > assigned to the property when the control is actually instantiated

    in
    > > > > the page's control hierarchy.
    > > > >
    > > > > Actually, your property should be storing the value of the property

    to
    > > > > the ViewState StateBag, so that this value can be changed
    > > > > programmatically...
    > > > >
    > > > > hth
    > > > >
    > > > > --
    > > > >
    > > > > Scott Mitchell
    > > > >
    > > > > http://www.4GuysFromRolla.com
    > > > > http://www.ASPFAQs.com
    > > > > http://www.ASPMessageboard.com
    > > > >
    > > > > * When you think ASP, think 4GuysFromRolla.com!
    > > >
    > > >

    > >
    > >

    >
    >
     
    Robert Koritnik, May 21, 2004
    #6
    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. dan
    Replies:
    0
    Views:
    549
  2. SJL
    Replies:
    2
    Views:
    2,376
  3. =?Utf-8?B?QVNQIERldmVsb3Blcg==?=

    System.ComponentModel.DefaultValue doesn't work with user control

    =?Utf-8?B?QVNQIERldmVsb3Blcg==?=, Sep 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,691
    =?Utf-8?B?QVNQIERldmVsb3Blcg==?=
    Sep 13, 2006
  4. caviar

    DefaultValue not working (string)

    caviar, Sep 17, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    150
    caviar
    Sep 18, 2003
  5. Shadow Lynx
    Replies:
    1
    Views:
    208
Loading...

Share This Page