CompositeControls: ViewState properties w/ Mapped properties probl

G

Guest

I have a CompositeControl with two types of properties:

1.) Mapped Properties that map directly to a child control's properties
(ex.: this.TextboxText = m_txt.Text). These properties are handled by their
underlying classes (such as the TextBox control), and are not persisted by me.
2.) Unique Properties that don't map directly and are persisted in ViewState
(ex.: this.LabelPosition, which specifies where on the form the label should
be rendered). These properties are applied to the relevant controls in
'OnPreRender()', and sometimes used in 'CreateChildControls()'.

I can only get one of these types of properties to work properly at one
time. If, in the second type of property (those stored in ViewState) I add
the line 'ChildControlsCreated = false;' after the Set statement, the
ViewState properties work properly and render with their assigned values, but
then controls of the first type no longer work, and are always reset to their
initial values.

If I remove the 'ChildControlsCreate = false;' line from the ViewState
controls, the first type (Mapped Properties) function properly, including
maintaining their values on Postback, but the second type (Unique/ViewState
properties) always get set to their default values, even when they are
explicitly set in code or in the HTML markup.

The problem is that I need, obviously, both property types to work, but
after a lot of effort (and several forum posts), am no closer to a solution.

If you would like a full code sample of this, let me know and I'll email a
VS2005 solution to you.

Christophe

Type 1 (Mapped Properties) --> Generally defined in a child class that
inherits from a common parent
--------------------------
/// <summary>
/// Gets or sets the textbox text.
/// </summary>
/// <value>The textbox text.</value>
[Bindable(true)]
[Category("Textbox")]
[DefaultValue(typeof(string), "")]
[Description("The text that will be displayed in the textbox.")]
[Localizable(true)]
[NotifyParentProperty(true)]
[RefreshProperties(RefreshProperties.All)]
public string Text
{
get
{
EnsureChildControls();
return (m_txt.Text == null) ?
string.Empty :
m_txt.Text;
}
set
{
Debug.Assert(value != null, "Warning: TextboxText property is null!");
if (value != null)
{
EnsureChildControls();
m_txt.Text = value;
}
else
{
throw new NullReferenceException("TextboxText can not be
assigned a null value.");
}
}
}

Type 2 (Unique Properties) --> Defined in a parent class that contains all
common elements
--------------------------
/// <summary>
/// Gets or sets the position of the descriptive label relative to the rest
of the control.
/// </summary>
/// <value>The label position.</value>
[Bindable(true)]
[Category("Label")]
[DefaultValue(Position.Left)]
[Localizable(false)]
[Description("The position of the descriptive label relative to the rest of
the control.")]
public Position LabelPosition
{
get
{
Position p = (Position)ViewState["LabelPosition"];
return p;
}
set
{
ViewState["LabelPosition"] = value;
// Toggle line to see effect
// ChildcontrolsCreate = false;
}
}

CreateChildControls() -> Code from parent class, SupportFormLabelledControl
(which handles all common elements)
---------------------
protected override void CreateChildControls()
{
// Clear the control collection
Controls.Clear();

// Any dependant controls used in this custom control must
// be added to the control 'hierarchy'. If they are not added
// to the control collection, they will not be visible to other
// controls on the page.

// Instantiate any dependant controls
m_tbl = new Table();
m_lbl = new CallbackLabel();
m_icn = new IconPopupControl();
m_plc = new PlaceHolder();

// Create table object and format it through relevant method
m_tbl = SharedFunctions.CreateLabelledControlTable(this.LabelPosition);

// Add table to the control collection
Controls.Add(m_tbl);

// Add controls to the table control collection
switch (this.LabelPosition)
{
case Position.Left:
m_tbl.Rows[0].Cells[0].Controls.Add(m_lbl);
m_tbl.Rows[0].Cells[1].Controls.Add(m_plc);
m_tbl.Rows[0].Cells[1].Controls.Add(m_icn);
// Set relevant design properties
m_tbl.Rows[0].Cells[0].Width = new
Unit(this.LabelWidth.ToString());
m_tbl.Rows[0].Cells[0].VerticalAlign = this.LabelVerticalAlign;
break;
case Position.Top:
m_tbl.Rows[0].Cells[0].Controls.Add(m_lbl);
m_tbl.Rows[1].Cells[0].Controls.Add(m_plc);
m_tbl.Rows[1].Cells[0].Controls.Add(m_icn);
// Set relevant design properties
m_tbl.Rows[0].Cells[0].Width = new
Unit(this.LabelWidth.ToString());
m_tbl.Rows[0].Cells[0].VerticalAlign = this.LabelVerticalAlign;
break;
case Position.Right:
m_tbl.Rows[0].Cells[0].Controls.Add(m_plc);
m_tbl.Rows[0].Cells[0].Controls.Add(m_icn);
m_tbl.Rows[0].Cells[1].Controls.Add(m_lbl);
// Set relevant design properties
m_tbl.Rows[0].Cells[1].Width = new
Unit(this.LabelWidth.ToString());
m_tbl.Rows[0].Cells[1].VerticalAlign = this.LabelVerticalAlign;
break;
case Position.Bottom:
m_tbl.Rows[0].Cells[0].Controls.Add(m_plc);
m_tbl.Rows[0].Cells[0].Controls.Add(m_icn);
m_tbl.Rows[1].Cells[0].Controls.Add(m_lbl);
// Set relevant design properties
m_tbl.Rows[1].Cells[0].Width = new
Unit(this.LabelWidth.ToString());
m_tbl.Rows[1].Cells[0].VerticalAlign = this.LabelVerticalAlign;
break;
default:
Debug.Assert(false);
break;
}

// Call base method
base.CreateChildControls();
}

CreateChildControls() -> Code from inheriting class, which specializes the
top-level class (i.e., SupportTextBox)
---------------------
protected override void CreateChildControls()
{
// Call base method (create the underlying table and common controls)
base.CreateChildControls();

// Instantiate any dependant controls
m_txt = new CallbackTextBox();

// Register any events associated with dependant controls
m_txt.TextChanged += new EventHandler(RaiseTextChanged);
m_icn.ImageMouseDown += new EventHandler(this.RaiseIconMouseDown);

// Add unique controls to the base class placeholder
m_plc.Controls.Add(m_txt);
}


OnPreRender() --> Parent Class (SupportFormLabelledControl)
-------------
contains the event data.</param>
protected override void OnPreRender(EventArgs e)
{
// Call base method
base.OnPreRender(e);

// Add reference to embedded CSS file
if (!(Page == null))
{
if (!(Page.ClientScript.IsClientScriptBlockRegistered("CssStyles")))
{
string cssLocation = this.Page.ClientScript.GetWebResourceUrl(
this.GetType(),
"CompanyName.EEE.Web.UI.Resources.Styles.css");
string cssLink = @"<!-- Css Stylesheet -->" + "\r\n";
cssLink += @"<link href='" + cssLocation + "' rel='stylesheet'
type='text/css' />" + "\r\n";
Page.ClientScript.RegisterClientScriptBlock(
typeof(SupportFormLabelledControl),
"CssStyles",
cssLink);
}
}

// Associate dependent control properties with this control's properties
m_lbl.CssClass = this.LabelCssClass;
m_lbl.Text = this.LabelText;
m_lbl.Visible = this.LabelVisible;
m_lbl.RadControlsDir = this.ScriptsPath;
m_lbl.CallbackEnabled = this.CallbackEnabled;
m_lbl.DisableAtCallback = this.DisableAtCallback;
m_lbl.Enabled = this.Enabled;
m_icn.CallbackEnabled = this.CallbackEnabled;
m_icn.DisableAtCallback = this.DisableAtCallback;
m_icn.WarningImageUrl = this.WarningImageUrl;
m_icn.ImageAlign = this.ImageAlign;
m_icn.EmptyImageUrl = this.EmptyImageUrl;
m_icn.MessageStyle = this.MessageStyle;
m_icn.PopupText = this.PopupText;
m_icn.PopupTextResourceKey = this.PopupTextResourceKey;
m_icn.PopupTitle = this.PopupTitle;
m_icn.PopupTitleResourceKey = this.PopupTitleResourceKey;
m_icn.LinkUrl = this.LinkUrl;
m_icn.Enabled = this.Enabled;
m_icn.CssClass = this.WarningIconCssStyle;

// Enable or disable warning icon as appropriate
m_icn.Visible = this.Required ? true : false;
}

OnPreRender() --> Specialized, Inheriting Class (SupportTextBox)
-------------
protected override void OnPreRender(EventArgs e)
{
// Call base method (common fields like m_lbl and m_icn are handled here)
base.OnPreRender(e);

// Associate dependent control properties with this control's properties
m_txt.MaxLength = this.MaxLength;
m_txt.ReadOnly = this.ReadOnly;
m_txt.RadControlsDir = this.ScriptsPath;
m_txt.DisableAtCallback = this.DisableAtCallback;
m_txt.CallbackEnabled = this.CallbackEnabled;
m_txt.CssClass = this.TextboxCssClass;
m_txt.Enabled = this.Enabled;
m_txt.Text = this.Text;
m_txt.TextMode = this.TextMode;
m_txt.Width = this.TextboxWidth;
m_txt.Rows = this.Rows;
}
 
S

Steven Cheng[MSFT]

Hi Christophe,

Regarding on this issue, I've posted my response and suggestion in the
below thread:

Subject: ViewState properties and mapped properties don't work well together
Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols

Please feel free to post there if you meet any further problem..

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


--------------------
| Thread-Topic: CompositeControls: ViewState properties w/ Mapped
properties probl
| thread-index: AcYc1IDjPQ5qSRwpSquoNZe7RkwGMQ==
| X-WBNR-Posting-Host: 193.172.19.20
| From: =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?=
<[email protected]>
| Subject: CompositeControls: ViewState properties w/ Mapped properties
probl
| Date: Thu, 19 Jan 2006 00:44:04 -0800
| Lines: 266
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:372014
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I have a CompositeControl with two types of properties:
|
| 1.) Mapped Properties that map directly to a child control's properties
| (ex.: this.TextboxText = m_txt.Text). These properties are handled by
their
| underlying classes (such as the TextBox control), and are not persisted
by me.
| 2.) Unique Properties that don't map directly and are persisted in
ViewState
| (ex.: this.LabelPosition, which specifies where on the form the label
should
| be rendered). These properties are applied to the relevant controls in
| 'OnPreRender()', and sometimes used in 'CreateChildControls()'.
|
| I can only get one of these types of properties to work properly at one
| time. If, in the second type of property (those stored in ViewState) I
add
| the line 'ChildControlsCreated = false;' after the Set statement, the
| ViewState properties work properly and render with their assigned values,
but
| then controls of the first type no longer work, and are always reset to
their
| initial values.
|
| If I remove the 'ChildControlsCreate = false;' line from the ViewState
| controls, the first type (Mapped Properties) function properly, including
| maintaining their values on Postback, but the second type
(Unique/ViewState
| properties) always get set to their default values, even when they are
| explicitly set in code or in the HTML markup.
|
| The problem is that I need, obviously, both property types to work, but
| after a lot of effort (and several forum posts), am no closer to a
solution.
|
| If you would like a full code sample of this, let me know and I'll email
a
| VS2005 solution to you.
|
| Christophe
|
| Type 1 (Mapped Properties) --> Generally defined in a child class that
| inherits from a common parent
| --------------------------
| /// <summary>
| /// Gets or sets the textbox text.
| /// </summary>
| /// <value>The textbox text.</value>
| [Bindable(true)]
| [Category("Textbox")]
| [DefaultValue(typeof(string), "")]
| [Description("The text that will be displayed in the textbox.")]
| [Localizable(true)]
| [NotifyParentProperty(true)]
| [RefreshProperties(RefreshProperties.All)]
| public string Text
| {
| get
| {
| EnsureChildControls();
| return (m_txt.Text == null) ?
| string.Empty :
| m_txt.Text;
| }
| set
| {
| Debug.Assert(value != null, "Warning: TextboxText property is
null!");
| if (value != null)
| {
| EnsureChildControls();
| m_txt.Text = value;
| }
| else
| {
| throw new NullReferenceException("TextboxText can not be
| assigned a null value.");
| }
| }
| }
|
| Type 2 (Unique Properties) --> Defined in a parent class that contains
all
| common elements
| --------------------------
| /// <summary>
| /// Gets or sets the position of the descriptive label relative to the
rest
| of the control.
| /// </summary>
| /// <value>The label position.</value>
| [Bindable(true)]
| [Category("Label")]
| [DefaultValue(Position.Left)]
| [Localizable(false)]
| [Description("The position of the descriptive label relative to the rest
of
| the control.")]
| public Position LabelPosition
| {
| get
| {
| Position p = (Position)ViewState["LabelPosition"];
| return p;
| }
| set
| {
| ViewState["LabelPosition"] = value;
| // Toggle line to see effect
| // ChildcontrolsCreate = false;
| }
| }
|
| CreateChildControls() -> Code from parent class,
SupportFormLabelledControl
| (which handles all common elements)
| ---------------------
| protected override void CreateChildControls()
| {
| // Clear the control collection
| Controls.Clear();
|
| // Any dependant controls used in this custom control must
| // be added to the control 'hierarchy'. If they are not added
| // to the control collection, they will not be visible to other
| // controls on the page.
|
| // Instantiate any dependant controls
| m_tbl = new Table();
| m_lbl = new CallbackLabel();
| m_icn = new IconPopupControl();
| m_plc = new PlaceHolder();
|
| // Create table object and format it through relevant method
| m_tbl =
SharedFunctions.CreateLabelledControlTable(this.LabelPosition);
|
| // Add table to the control collection
| Controls.Add(m_tbl);
|
| // Add controls to the table control collection
| switch (this.LabelPosition)
| {
| case Position.Left:
| m_tbl.Rows[0].Cells[0].Controls.Add(m_lbl);
| m_tbl.Rows[0].Cells[1].Controls.Add(m_plc);
| m_tbl.Rows[0].Cells[1].Controls.Add(m_icn);
| // Set relevant design properties
| m_tbl.Rows[0].Cells[0].Width = new
| Unit(this.LabelWidth.ToString());
| m_tbl.Rows[0].Cells[0].VerticalAlign =
this.LabelVerticalAlign;
| break;
| case Position.Top:
| m_tbl.Rows[0].Cells[0].Controls.Add(m_lbl);
| m_tbl.Rows[1].Cells[0].Controls.Add(m_plc);
| m_tbl.Rows[1].Cells[0].Controls.Add(m_icn);
| // Set relevant design properties
| m_tbl.Rows[0].Cells[0].Width = new
| Unit(this.LabelWidth.ToString());
| m_tbl.Rows[0].Cells[0].VerticalAlign =
this.LabelVerticalAlign;
| break;
| case Position.Right:
| m_tbl.Rows[0].Cells[0].Controls.Add(m_plc);
| m_tbl.Rows[0].Cells[0].Controls.Add(m_icn);
| m_tbl.Rows[0].Cells[1].Controls.Add(m_lbl);
| // Set relevant design properties
| m_tbl.Rows[0].Cells[1].Width = new
| Unit(this.LabelWidth.ToString());
| m_tbl.Rows[0].Cells[1].VerticalAlign =
this.LabelVerticalAlign;
| break;
| case Position.Bottom:
| m_tbl.Rows[0].Cells[0].Controls.Add(m_plc);
| m_tbl.Rows[0].Cells[0].Controls.Add(m_icn);
| m_tbl.Rows[1].Cells[0].Controls.Add(m_lbl);
| // Set relevant design properties
| m_tbl.Rows[1].Cells[0].Width = new
| Unit(this.LabelWidth.ToString());
| m_tbl.Rows[1].Cells[0].VerticalAlign =
this.LabelVerticalAlign;
| break;
| default:
| Debug.Assert(false);
| break;
| }
|
| // Call base method
| base.CreateChildControls();
| }
|
| CreateChildControls() -> Code from inheriting class, which specializes
the
| top-level class (i.e., SupportTextBox)
| ---------------------
| protected override void CreateChildControls()
| {
| // Call base method (create the underlying table and common controls)
| base.CreateChildControls();
|
| // Instantiate any dependant controls
| m_txt = new CallbackTextBox();
|
| // Register any events associated with dependant controls
| m_txt.TextChanged += new EventHandler(RaiseTextChanged);
| m_icn.ImageMouseDown += new EventHandler(this.RaiseIconMouseDown);
|
| // Add unique controls to the base class placeholder
| m_plc.Controls.Add(m_txt);
| }
|
|
| OnPreRender() --> Parent Class (SupportFormLabelledControl)
| -------------
| contains the event data.</param>
| protected override void OnPreRender(EventArgs e)
| {
| // Call base method
| base.OnPreRender(e);
|
| // Add reference to embedded CSS file
| if (!(Page == null))
| {
| if
(!(Page.ClientScript.IsClientScriptBlockRegistered("CssStyles")))
| {
| string cssLocation = this.Page.ClientScript.GetWebResourceUrl(
| this.GetType(),
| "CompanyName.EEE.Web.UI.Resources.Styles.css");
| string cssLink = @"<!-- Css Stylesheet -->" + "\r\n";
| cssLink += @"<link href='" + cssLocation + "'
rel='stylesheet'
| type='text/css' />" + "\r\n";
| Page.ClientScript.RegisterClientScriptBlock(
| typeof(SupportFormLabelledControl),
| "CssStyles",
| cssLink);
| }
| }
|
| // Associate dependent control properties with this control's
properties
| m_lbl.CssClass = this.LabelCssClass;
| m_lbl.Text = this.LabelText;
| m_lbl.Visible = this.LabelVisible;
| m_lbl.RadControlsDir = this.ScriptsPath;
| m_lbl.CallbackEnabled = this.CallbackEnabled;
| m_lbl.DisableAtCallback = this.DisableAtCallback;
| m_lbl.Enabled = this.Enabled;
| m_icn.CallbackEnabled = this.CallbackEnabled;
| m_icn.DisableAtCallback = this.DisableAtCallback;
| m_icn.WarningImageUrl = this.WarningImageUrl;
| m_icn.ImageAlign = this.ImageAlign;
| m_icn.EmptyImageUrl = this.EmptyImageUrl;
| m_icn.MessageStyle = this.MessageStyle;
| m_icn.PopupText = this.PopupText;
| m_icn.PopupTextResourceKey = this.PopupTextResourceKey;
| m_icn.PopupTitle = this.PopupTitle;
| m_icn.PopupTitleResourceKey = this.PopupTitleResourceKey;
| m_icn.LinkUrl = this.LinkUrl;
| m_icn.Enabled = this.Enabled;
| m_icn.CssClass = this.WarningIconCssStyle;
|
| // Enable or disable warning icon as appropriate
| m_icn.Visible = this.Required ? true : false;
| }
|
| OnPreRender() --> Specialized, Inheriting Class (SupportTextBox)
| -------------
| protected override void OnPreRender(EventArgs e)
| {
| // Call base method (common fields like m_lbl and m_icn are handled
here)
| base.OnPreRender(e);
|
| // Associate dependent control properties with this control's
properties
| m_txt.MaxLength = this.MaxLength;
| m_txt.ReadOnly = this.ReadOnly;
| m_txt.RadControlsDir = this.ScriptsPath;
| m_txt.DisableAtCallback = this.DisableAtCallback;
| m_txt.CallbackEnabled = this.CallbackEnabled;
| m_txt.CssClass = this.TextboxCssClass;
| m_txt.Enabled = this.Enabled;
| m_txt.Text = this.Text;
| m_txt.TextMode = this.TextMode;
| m_txt.Width = this.TextboxWidth;
| m_txt.Rows = this.Rows;
| }
|
|
|
 

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,828
Messages
2,569,736
Members
45,518
Latest member
vlonesshirt

Latest Threads

Top