Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > ViewState properties and mapped properties don't work well togethe

Reply
Thread Tools

ViewState properties and mapped properties don't work well togethe

 
 
Christophe Peillet
Guest
Posts: n/a
 
      01-18-2006
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;
}

 
Reply With Quote
 
 
 
 
pH
Guest
Posts: n/a
 
      01-18-2006
I think you'll get a much better respose if you re-post this on
microsoft.public.dotnet.framework.aspnet [not .buildingcontrols], as
that group has a much larger readership than this, and should be able
to answer your question...

 
Reply With Quote
 
 
 
 
agapeton@gmail.com
Guest
Posts: n/a
 
      01-21-2006
You wil also want to shorten that... I can't imagine anyone reading all
that. If it's more than 200 words (with code), then most people give
up. You do not need to post the planet, only the context.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does "additivity" flag in log4j.properties work only with log4j or with Apache Commons Logging as well? Gianni Galore Java 0 09-08-2010 09:26 AM
CompositeControls: ViewState properties w/ Mapped properties probl =?Utf-8?B?Q2hyaXN0b3BoZSBQZWlsbGV0?= ASP .Net 1 01-19-2006 09:19 AM
ViewState properties and mapped properties don't work well togethe Christophe Peillet ASP .Net Web Controls 1 01-19-2006 09:01 AM
Mapped Network drives to local folders work only when online =?Utf-8?B?cnlhbnZy?= Wireless Networking 0 05-12-2005 07:16 PM
Hi Whats considered to be a well priced and well reviewed plasma tv 42 inch and above ? Hmmmmmmm DVD Video 8 09-28-2004 09:32 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57