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

Discussion in 'ASP .Net Building Controls' started by gwellman, Dec 2, 2003.

  1. gwellman

    gwellman Guest

    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
    gwellman, Dec 2, 2003
    #1
    1. Advertising

  2. gwellman

    Wayne Guest

    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.



    "gwellman" <> wrote in message
    news:...
    > 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
    Wayne, Dec 3, 2003
    #2
    1. Advertising

  3. "gwellman" <> wrote in message
    news:...
    > 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;}
    }

    --
    John Saunders
    John.Saunders at SurfControl.com
    John Saunders, Dec 3, 2003
    #3
  4. gwellman

    gwellman Guest

    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 :)
    gwellman, Dec 4, 2003
    #4
    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. OscarC
    Replies:
    7
    Views:
    846
    Roedy Green
    May 9, 2004
  2. Jaime Wyant

    I don't quite get this "string".find()

    Jaime Wyant, Nov 11, 2004, in forum: Python
    Replies:
    8
    Views:
    295
    Caleb Hattingh
    Nov 12, 2004
  3. Patrick M.

    I don't quite understand this exercise...

    Patrick M., Sep 26, 2005, in forum: C Programming
    Replies:
    10
    Views:
    591
    Keith Thompson
    Sep 26, 2005
  4. Dan
    Replies:
    5
    Views:
    525
    Bikal KC
    Aug 23, 2007
  5. Jedrin
    Replies:
    1
    Views:
    513
    sreservoir
    Feb 18, 2012
Loading...

Share This Page