Don't quite understand how to create a default property value, please help

G

gwellman

I'm sure the answer is something simple, I'm just new to this
environment...

I made a custom control (code to follow). I can register the control
with the toolbox and use it on a page. If I enter property values
*different* from the default values, they get put in the HTML, and
they work. If I put in the default values, they don't get put in the
HTML (fair enough, that would be redundant), but then at run-time,
it's like there is no default. All my labels are blank! Do I need to
put in code that explictly checks to see if property values were sent
in from the HTML and puts in the default values if not? That also
seems redundant.

Ok, code, with some unecessary stuff stripped (like the "using"
statements)

/// <summary>
/// A control to allow users to change their password.
/// </summary>
[ToolboxData("<{0}:ChangePassword runat=server></{0}:ChangePassword]
public class ChangePassword : WebControl, INamingContainer
{
/// <summary>
/// Create new instance
/// </summary>
public ChangePassword()
:base(HtmlTextWriterTag.Table)
{
}
}
/// <summary>
/// Gets and sets the value of the first label (old password)
/// </summary>
[Category("Appearance"),
DefaultValue("Old Password"),
Browsable(true)]
public virtual String OldPasswordLabel
{
get {return StringLibrary.NullToEmpty((string)ViewState["OldPasswordLabel"]);}
set {ViewState["OldPasswordLabel"] = value;}
}

/// <summary>
/// Gets and sets the value of the second label (new password)
/// </summary>
[Category("Appearance"),
DefaultValue("New Password"),
Browsable(true)]
public virtual String NewPasswordLabel1
{
get {return StringLibrary.NullToEmpty((string)ViewState["NewPasswordLabel1"]);}
set {ViewState["NewPasswordLabel1"] = value;}
}

<snip some more properties>

/// <summary>
/// Overrides <see cref="Control.CreateChildControls"/>
/// </summary>
protected override void CreateChildControls()
{
lblOldPass = new Label();
lblOldPass.EnableViewState = false;
lblOldPass.Text = OldPasswordLabel;
Controls.Add(lblOldPass);
lblNewPass1 = new Label();
lblNewPass1.EnableViewState = false;
lblNewPass1.Text = NewPasswordLabel1;
Controls.Add(lblNewPass1);

<etc.>

I've tried this a few different ways. I tried having the public
properties directly access the properties of the child objects,
without using ViewState. I've tried local variables ...

I'm sure it's just something simple I don't understand about the
environment.

Thanks in advance,
Greg
 
W

Wayne

Someone may have a different answer but the server controls we make the
DefaultValue set as well as we change the Get of the property so that if the
viewstate is something we are expecting even if it is Nothing we set it
that way.
 
J

John Saunders

gwellman said:
I'm sure the answer is something simple, I'm just new to this
environment...

I made a custom control (code to follow). I can register the control
with the toolbox and use it on a page. If I enter property values
*different* from the default values, they get put in the HTML, and
they work. If I put in the default values, they don't get put in the
HTML (fair enough, that would be redundant), but then at run-time,
it's like there is no default. All my labels are blank! Do I need to
put in code that explictly checks to see if property values were sent
in from the HTML and puts in the default values if not? That also
seems redundant.

The DefaultValue attribute only informs the designer what the default value
is meant to be. This determines whether the value is bolded in the Property
Grid and whether it shows up in the HTML.

However, it has nothing at all to do with the runtime behavior of your
control. You'll want to do something like the following:

public object SomeProperty
{
get {return ViewState["SomeProperty"] == null ? "default value" :
ViewState["SomeProperty"];}
set {ViewState["SomeProperty"] = value;}
}
 
G

gwellman

Thanks for the help. I guess I just wasn't expecting the redundancy
of having to specify the default twice - once for the designer and
once to actually happen.

I have a new, closely related problem with the same code. Imagine
that looking up the value of some property (like those simple
label.text values) happened to be expensive (it isn't in this case,
but just bear with me). Then wouldn't one only set the property value
if it wasn't in a PostBack situation? If it's a postback, then the
value should be in the viewstate (because we put it there) so the
property should just continue to report its old value, right?

I'm finding this doesn't work.

TIA again :)
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top