473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

CompositeControls: ViewState properties w/ Mapped properties probl

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.La belPosition);

// 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;
}
Jan 19 '06 #1
1 1638
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.webcontro ls

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?=
<Ch***************@nospam.nospam>
| Subject: CompositeControls: ViewState properties w/ Mapped properties
probl
| Date: Thu, 19 Jan 2006 00:44:04 -0800
| Lines: 266
| Message-ID: <E7**********************************@microsoft.co m>
| 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.La belPosition);
|
| // 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;
| }
|
|
|

Jan 19 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mike | last post by:
hi to all does any body know or receive this message before and how i can repare this: The viewstate is invalid for this page and might be corrupted. thank's Mike
1
by: Simon | last post by:
Hi everyone, I have a quick question that I hope someone can help me with: I've made a user control that contains a text box and some validation functionality. This control has a few extra...
10
by: Robert | last post by:
I have an app that was originally 1.1, now migrated to 2.0 and have run into some sporadic viewstate errors...usually saying the viewstate is invalid, eventvalidation failed or mac error. My web...
6
by: hitendra15 | last post by:
Hi I have created web user control which has Repeater control and Linkbutton in ItemTemplate of repeater control, following is the code for this control On first load it runs fine but when...
5
by: Jeronimo Bertran | last post by:
Building a web control library using VS2005, the WebCustomControls are automatically added to the toolbox but the CompositeControls are not. Is there a way to make the CompositeControls...
2
by: Carly | last post by:
Hi, I am now not sure I understand what ViewState does. Having EnableViewState=true or false on a WEB form and/or different server controls does not seem to make any difference. I am just...
5
by: AlexC | last post by:
Hi, i have just read some threads about viewstate and wanted to test myself. But there must be something i don't undertstand, because when i submit the form with EnableViewState="false", i get...
12
by: Nick C | last post by:
Hi How can i reduce the viewstate for my asp.net application. It is getting very large now. What is a good solution? thanks N
3
by: shapper | last post by:
Hello, I would like some confirmation on events. Should controls be added to page or to their parent controls in Page.Load event wrapped by "If Not PostBack"? And in which event should the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.