DefaultValue is not initialized

O

Oleg Slyusarchuk

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
 
S

Scott Mitchell [MVP]

Oleg said:
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
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 
O

Oleg Slyusarchuk

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
 
R

Robert Koritnik

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.
 
O

Oleg Slyusarchuk

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
 
R

Robert Koritnik

You can still do that programaticly. You can check for the
DefaultValueAttribute and do the appropriate thing.

RobertK.
 

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

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top