Ask a Question related to ASP.NET Building Controls, Design and Development.
-
Christophe Peillet #1
ViewState properties and mapped properties don't work well togethe
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;
}
Christophe Peillet Guest
-
Making Custom Control Properties Visible in Visual Studio's Properties Palette
I am learning how to use the System.ComponentModel class in VB.NET so that I can add my ASP.NET controls to Visual Studio .NET 2003. I have managed... -
Composite Control - Viewstate management for complex properties
I have seen several composite control examples where LoadViewState and SaveViewState are overloaded to track viewstate on complex objects such as... -
Properties, JavaScript and Viewstate
Hi all! I´m trying to build a custom panel control that has the ability to expand or collapse at runtime without postback. I switch the... -
Illustrator properties vs FreeHand properties
I have to say that after playing around with Illustrator CS, FreeHand wins hands down as far as simplicity goes. I can't believe how many flipping... -
properties not showing in properties window at design time
I created a usercontrol using vb but the properties will not show in the properties pane however the properties show up in the intellisence list.... -
pH #2
Re: ViewState properties and mapped properties don't work well togethe
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...
pH Guest
-
agapeton@gmail.com #3
Re: ViewState properties and mapped properties don't work well togethe
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.
agapeton@gmail.com Guest



Reply With Quote

