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