C
Christophe Peillet
I am in the process of developing several custom controls that have
validation and Ajax/Callback support built into them, and have finished most
of the work, but have a stubborn problem when running in DesignMode.
When I try to modify the value of certain properties, the Html code is
modified with the new property value (for example '... CheckboxText="MyText"
.....'), but the visual display is never updated, and the property explorer
window also doesn't reflect the new value. It behaves as if the property is
read only, and allow no apparent change, even if it is changing the values
beneath the surface, and the changes are visible at RunTime.
I suspect the problem is that I have not properly understand the page model
properly, and am putting something in the wrong place. (This is my first
Custom Control that is more than something simplistic.) If someone can take
a look specifically at the methods SetDesignTimeProperties() and
SetRunTimeProperties(), I may be running this in the wrong place, but am not
sure where else to run them, etc. I will also include the code for the
designer in the next post (due to size restrictions). The Custom control is
as follows:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Diagnostics;
using Telerik.WebControls;
namespace CompanyName.EEE.Web.UI.FormControls
{
/// <summary>
/// An AJAX enabled CheckBox control, which automatically applies the
CompanyName style guide to its appearance.
/// </summary>
/// <seealso cref="T:SupportCheckBoxDesigner"/>
/// <seealso cref="T:SupportCheckBoxActionList"/>
[Designer(typeof(SupportCheckBoxDesigner))]
[DefaultProperty("Text")]
[DefaultEvent("OnCheckedChanged")]
[ToolboxData("<{0}:SupportCheckBox
runat=\"server\"></{0}:SupportCheckBox>")]
[ParseChildren(true)]
[PersistChildren(false)]
[ControlValueProperty("CheckboxChecked")]
public class SupportCheckBox : SupportFormLabelledControl,
INamingContainer
{
#region Private Fields
// Any third-party or external controls that you wish to add to the
this
// control, such as buttons, labels, etc., should be declared here.
// Declare any required controls/objects
private CallbackLabel m_lbl;
private CallbackCheckBox m_chk;
private IconPopupControl m_icn;
#endregion
#region Event Handlers
// Declare any events that will be raised by this control.
/// <summary>
/// Occurs when the value of the Checked property changes. (This
event only fires when <see cref="P:SupportCheckBox.CallbackEnabled"/> is set
to True.)
/// </summary>
[Category("Action")]
[Description("Occurs when the value of the Checked property changes.
(Please note that this event only fires when CallbackEnabled is set to
True.)")]
public event EventHandler CheckedChanged;
#endregion
#region Constructor
// Default values for properties should ONLY be defined in the the
// class constructor. If you set properties elsewhere, such as in the
// OnLoad event, you will make the control insensitive to external,
// tag-level settings.
/// <summary>
/// Initializes a new instance of the <see cref="SupportCheckBox"/>
class.
/// </summary>
public SupportCheckBox()
{
// Set control default values
this.SetDefaultValues();
}
#endregion
#region Protected Methods (Page Events)
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"></see> object
that contains the event data.</param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs"></see> object
that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
// This method captures the controls Load event (declared and
bound in the
// class constructor). RunTime rendering takes place in this
method (as
// opposed to DesignMode rendering, which takes place elsewhere).
// Call base Load method
base.OnLoad(e);
EnsureChildControls();
// Make sure we are not running in DesignMode
if (!(this.DesignMode))
{
// Associate dependent control properties with this
control's properties
this.SetRunTimeProperties();
}
}
/// <summary>
/// Called by the ASP.NET page framework to notify server controls
that use
/// composition-based implementation to create any child controls
they contain
/// in preparation for posting back or rendering.
/// </summary>
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 dependent objects
m_lbl = new CallbackLabel();
m_chk = new CallbackCheckBox();
m_icn = new IconPopupControl();
// Register any event associated with dependant objects
m_chk.CheckedChanged += new
EventHandler(this.RaiseCheckedChanged);
// Add them to the control collection
Controls.Add(m_lbl);
Controls.Add(m_chk);
Controls.Add(m_icn);
// Call base method
base.CreateChildControls();
}
/// <summary>
/// Renders the contents of the control to the specified writer.
This method is used primarily by control developers.
/// </summary>
/// <param name="output">A <see
cref="T:System.Web.UI.HtmlTextWriter"></see> that represents the output
stream to render HTML content on the client.</param>
protected override void RenderContents(HtmlTextWriter output)
{
EnsureChildControls();
// Render temporary values if running in DesignMode. This
allows users
// to see the control as it will appear in the RunTime
environment.
if (this.DesignMode)
{
this.SetDesignTimeProperties();
}
// Create temporary HtmlTextWriter placeholder
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
// Create table to hold results
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Table);
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
// Render dependent controls
switch (this.LabelPosition)
{
case Position.Left:
// Label
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Style,
"width: " + this.LabelWidth.ToString() + ";");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_lbl.RenderControl(htmlWriter);
htmlWriter.RenderEndTag();
// Checkbox
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_chk.RenderControl(htmlWriter);
// Render the popup icon control if required
if (this.Required)
{
m_icn.RenderControl(htmlWriter);
}
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
break;
case Position.Top:
// Label
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Style,
"width: " + this.LabelWidth.ToString() + ";");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_lbl.RenderControl(htmlWriter);
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
// Checkbox
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Colspan,
"2");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_chk.RenderControl(htmlWriter);
// Render the popup icon control if required
if (this.Required)
{
m_icn.RenderControl(htmlWriter);
}
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
break;
case Position.Right:
// Checkbox
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_chk.RenderControl(htmlWriter);
// Render the popup icon control if required
if (this.Required)
{
m_icn.RenderControl(htmlWriter);
}
htmlWriter.RenderEndTag();
// Label
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Style,
"width: " + this.LabelWidth.ToString() + ";");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_lbl.RenderControl(htmlWriter);
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
break;
case Position.Bottom:
// Checkbox
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_chk.RenderControl(htmlWriter);
// Render the popup icon control if required
if (this.Required)
{
m_icn.RenderControl(htmlWriter);
}
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
// Label
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Colspan,
"2");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_lbl.RenderControl(htmlWriter);
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
break;
default:
Debug.Assert(false);
break;
}
// Close table tag
htmlWriter.RenderEndTag();
// If you wish to make any modifications to the raw Html code
// before it is send to the output stream (for example, ensuring
// that the code is XHtml compliant, etc.), you can make the
// modifications to the 'rawHtml' field below. This code will
// then be sent to the real Html output stream.
string rawHtml = stringBuilder.ToString();
output.Write(rawHtml);
}
/// <summary>
/// Fires when the value of the Checked property changes. This
event only fires when <see cref="P:SupportCheckBox.CallbackEnabled"/> is set
to True.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance
containing the event data.</param>
protected virtual void RaiseCheckedChanged(object sender, EventArgs e)
{
// ToDo: Is there a better way to do this (update properties
automatically)
this.CheckboxChecked = ((CallbackCheckBox)sender).Checked;
// Ensure that the event has a subscriber. If not, don't fire it.
if (CheckedChanged != null)
{
// Raise the CheckedChanged event
CheckedChanged(this, e);
}
}
#endregion
#region Private Methods
/// <summary>
/// Sets the control's default values.
/// </summary>
private void SetDefaultValues()
{
// Set properties to default values
this.CheckboxChecked = false;
this.CheckboxCssClass = SharedConstants.SupportForm_CheckBoxStyle;
this.CheckboxEnabled = true;
this.CheckboxText = string.Empty;
this.CheckboxTextResourceKey = string.Empty;
this.CheckboxVisible = true;
}
/// <summary>
/// Sets the properties of this control when executing in a design
time environment.
/// </summary>
private void SetDesignTimeProperties()
{
// Associate dependent control properties with this control's
properties
m_lbl.CssClass = this.LabelCssClass;
m_lbl.Text = this.LabelText == string.Empty ?
m_lbl.Text = "[LabelText]" :
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_chk.RadControlsDir = this.ScriptsPath;
m_chk.CallbackEnabled = this.CallbackEnabled;
m_chk.DisableAtCallback = this.DisableAtCallback;
m_chk.Enabled = this.CheckboxEnabled;
m_chk.CssClass = this.CheckboxCssClass;
m_chk.Text = this.CheckboxText == string.Empty ?
m_chk.Text = "[CheckboxText]" :
m_chk.Text = this.CheckboxText;
m_chk.Visible = this.CheckboxVisible;
m_chk.Checked = this.CheckboxChecked;
m_chk.AutoPostBack = this.AutoPostback;
m_chk.Enabled = this.Enabled;
m_icn.ImageUrl = 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.WarningIconVisible = this.WarningIconVisible;
m_icn.CallbackEnabled = this.CallbackEnabled;
m_icn.DisableAtCallback = this.DisableAtCallback;
m_icn.Enabled = this.Enabled;
m_icn.CssClass = this.WarningIconCssStyle;
}
/// <summary>
/// Sets the properties of this control when executing in a run time
environment.
/// </summary>
private void SetRunTimeProperties()
{
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_chk.RadControlsDir = this.ScriptsPath;
m_chk.CallbackEnabled = this.CallbackEnabled;
m_chk.DisableAtCallback = this.DisableAtCallback;
m_chk.Enabled = this.CheckboxEnabled;
m_chk.CssClass = this.CheckboxCssClass;
m_chk.Text = this.CheckboxText;
m_chk.Visible = this.CheckboxVisible;
m_chk.Checked = this.CheckboxChecked;
m_chk.AutoPostBack = this.AutoPostback;
m_chk.Enabled = this.Enabled;
m_icn.ImageUrl = 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.WarningIconVisible = this.WarningIconVisible;
m_icn.CallbackEnabled = this.CallbackEnabled;
m_icn.DisableAtCallback = this.DisableAtCallback;
m_icn.Enabled = this.Enabled;
m_icn.CssClass = this.WarningIconCssStyle;
}
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the checkbox CSS class.
/// </summary>
/// <value>The checkbox CSS class.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(string), "")]
[Description("CSS Class name applied to this control.")]
[Localizable(false)]
public string CheckboxCssClass
{
get
{
EnsureChildControls();
return (m_chk.CssClass == null) ?
string.Empty :
m_chk.CssClass;
}
set
{
Debug.Assert(value != null, "Warning: CheckboxCssClass
property is null!");
if (value != null)
{
EnsureChildControls();
m_chk.CssClass = value;
}
else
{
throw new NullReferenceException("CheckboxCssClass can
not be assigned a null value.");
}
}
}
/// <summary>
/// Gets or sets the checkbox text.
/// </summary>
/// <value>The checkbox text.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(string), "")]
[Description("The text that will be displayed with the checkbox.")]
[Localizable(true)]
[NotifyParentProperty(true)]
[Browsable(true)]
[RefreshProperties(RefreshProperties.All)]
public string CheckboxText
{
get
{
EnsureChildControls();
return (m_chk.Text == null) ?
string.Empty :
m_chk.Text;
}
set
{
Debug.Assert(value != null, "Warning: CheckboxText property
is null!");
if (value != null)
{
EnsureChildControls();
m_chk.Text = value;
}
else
{
throw new NullReferenceException("CheckboxText can not
be assigned a null value.");
}
}
}
/// <summary>
/// Gets or sets a value indicating whether the checkbox is enabled.
/// </summary>
/// <value><c>true</c> if checkbox enabled; otherwise,
<c>false</c>.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(bool), "True")]
[Description("Whether or not the Checkbox is enabled.")]
[Localizable(false)]
public bool CheckboxEnabled
{
get
{
EnsureChildControls();
return m_chk.Enabled;
}
set
{
EnsureChildControls();
m_chk.Enabled = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether the checkbox is checked.
/// </summary>
/// <value><c>true</c> if checkbox checked; otherwise,
<c>false</c>.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(bool), "False")]
[Description("Whether or not the Checkbox is selected (checked) or
not.")]
[Localizable(false)]
public bool CheckboxChecked
{
get
{
EnsureChildControls();
return m_chk.Checked;
}
set
{
EnsureChildControls();
m_chk.Checked = value;
}
}
/// <summary>
/// Gets or sets the checkbox text resource key.
/// </summary>
/// <value>The checkbox text resource key.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(string), "")]
[Description("The resource key used when localising the Checkboxes
text.")]
[Localizable(false)]
public string CheckboxTextResourceKey
{
get
{
string s = (string)ViewState["CheckboxTextResourceKey"];
return (s == null) ? String.Empty : s;
}
set
{
Debug.Assert(value != null, "Warning:
CheckboxTextResourceKey property is null!");
if (value != null)
{
ViewState["CheckboxTextResourceKey"] = value;
}
else
{
throw new
NullReferenceException("CheckboxTextResourceKey can not be assigned a null
value.");
}
}
}
/// <summary>
/// Gets or sets a value indicating whether the checkbox is visible
or not.
/// </summary>
/// <value><c>true</c> if checkbox visible; otherwise,
<c>false</c>.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(bool), "True")]
[Description("Whether or not the checkbox is visible.")]
[Localizable(false)]
public bool CheckboxVisible
{
get
{
EnsureChildControls();
return m_chk.Visible;
}
set
{
EnsureChildControls();
m_chk.Visible = value;
}
}
#endregion
}
}
validation and Ajax/Callback support built into them, and have finished most
of the work, but have a stubborn problem when running in DesignMode.
When I try to modify the value of certain properties, the Html code is
modified with the new property value (for example '... CheckboxText="MyText"
.....'), but the visual display is never updated, and the property explorer
window also doesn't reflect the new value. It behaves as if the property is
read only, and allow no apparent change, even if it is changing the values
beneath the surface, and the changes are visible at RunTime.
I suspect the problem is that I have not properly understand the page model
properly, and am putting something in the wrong place. (This is my first
Custom Control that is more than something simplistic.) If someone can take
a look specifically at the methods SetDesignTimeProperties() and
SetRunTimeProperties(), I may be running this in the wrong place, but am not
sure where else to run them, etc. I will also include the code for the
designer in the next post (due to size restrictions). The Custom control is
as follows:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.Web.UI.Design.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Diagnostics;
using Telerik.WebControls;
namespace CompanyName.EEE.Web.UI.FormControls
{
/// <summary>
/// An AJAX enabled CheckBox control, which automatically applies the
CompanyName style guide to its appearance.
/// </summary>
/// <seealso cref="T:SupportCheckBoxDesigner"/>
/// <seealso cref="T:SupportCheckBoxActionList"/>
[Designer(typeof(SupportCheckBoxDesigner))]
[DefaultProperty("Text")]
[DefaultEvent("OnCheckedChanged")]
[ToolboxData("<{0}:SupportCheckBox
runat=\"server\"></{0}:SupportCheckBox>")]
[ParseChildren(true)]
[PersistChildren(false)]
[ControlValueProperty("CheckboxChecked")]
public class SupportCheckBox : SupportFormLabelledControl,
INamingContainer
{
#region Private Fields
// Any third-party or external controls that you wish to add to the
this
// control, such as buttons, labels, etc., should be declared here.
// Declare any required controls/objects
private CallbackLabel m_lbl;
private CallbackCheckBox m_chk;
private IconPopupControl m_icn;
#endregion
#region Event Handlers
// Declare any events that will be raised by this control.
/// <summary>
/// Occurs when the value of the Checked property changes. (This
event only fires when <see cref="P:SupportCheckBox.CallbackEnabled"/> is set
to True.)
/// </summary>
[Category("Action")]
[Description("Occurs when the value of the Checked property changes.
(Please note that this event only fires when CallbackEnabled is set to
True.)")]
public event EventHandler CheckedChanged;
#endregion
#region Constructor
// Default values for properties should ONLY be defined in the the
// class constructor. If you set properties elsewhere, such as in the
// OnLoad event, you will make the control insensitive to external,
// tag-level settings.
/// <summary>
/// Initializes a new instance of the <see cref="SupportCheckBox"/>
class.
/// </summary>
public SupportCheckBox()
{
// Set control default values
this.SetDefaultValues();
}
#endregion
#region Protected Methods (Page Events)
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"></see> object
that contains the event data.</param>
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load"></see> event.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs"></see> object
that contains the event data.</param>
protected override void OnLoad(EventArgs e)
{
// This method captures the controls Load event (declared and
bound in the
// class constructor). RunTime rendering takes place in this
method (as
// opposed to DesignMode rendering, which takes place elsewhere).
// Call base Load method
base.OnLoad(e);
EnsureChildControls();
// Make sure we are not running in DesignMode
if (!(this.DesignMode))
{
// Associate dependent control properties with this
control's properties
this.SetRunTimeProperties();
}
}
/// <summary>
/// Called by the ASP.NET page framework to notify server controls
that use
/// composition-based implementation to create any child controls
they contain
/// in preparation for posting back or rendering.
/// </summary>
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 dependent objects
m_lbl = new CallbackLabel();
m_chk = new CallbackCheckBox();
m_icn = new IconPopupControl();
// Register any event associated with dependant objects
m_chk.CheckedChanged += new
EventHandler(this.RaiseCheckedChanged);
// Add them to the control collection
Controls.Add(m_lbl);
Controls.Add(m_chk);
Controls.Add(m_icn);
// Call base method
base.CreateChildControls();
}
/// <summary>
/// Renders the contents of the control to the specified writer.
This method is used primarily by control developers.
/// </summary>
/// <param name="output">A <see
cref="T:System.Web.UI.HtmlTextWriter"></see> that represents the output
stream to render HTML content on the client.</param>
protected override void RenderContents(HtmlTextWriter output)
{
EnsureChildControls();
// Render temporary values if running in DesignMode. This
allows users
// to see the control as it will appear in the RunTime
environment.
if (this.DesignMode)
{
this.SetDesignTimeProperties();
}
// Create temporary HtmlTextWriter placeholder
StringBuilder stringBuilder = new StringBuilder();
StringWriter stringWriter = new StringWriter(stringBuilder);
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
// Create table to hold results
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Table);
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
// Render dependent controls
switch (this.LabelPosition)
{
case Position.Left:
// Label
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Style,
"width: " + this.LabelWidth.ToString() + ";");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_lbl.RenderControl(htmlWriter);
htmlWriter.RenderEndTag();
// Checkbox
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_chk.RenderControl(htmlWriter);
// Render the popup icon control if required
if (this.Required)
{
m_icn.RenderControl(htmlWriter);
}
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
break;
case Position.Top:
// Label
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Style,
"width: " + this.LabelWidth.ToString() + ";");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_lbl.RenderControl(htmlWriter);
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
// Checkbox
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Colspan,
"2");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_chk.RenderControl(htmlWriter);
// Render the popup icon control if required
if (this.Required)
{
m_icn.RenderControl(htmlWriter);
}
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
break;
case Position.Right:
// Checkbox
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_chk.RenderControl(htmlWriter);
// Render the popup icon control if required
if (this.Required)
{
m_icn.RenderControl(htmlWriter);
}
htmlWriter.RenderEndTag();
// Label
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Style,
"width: " + this.LabelWidth.ToString() + ";");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_lbl.RenderControl(htmlWriter);
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
break;
case Position.Bottom:
// Checkbox
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_chk.RenderControl(htmlWriter);
// Render the popup icon control if required
if (this.Required)
{
m_icn.RenderControl(htmlWriter);
}
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
// Label
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Tr);
htmlWriter.AddAttribute(HtmlTextWriterAttribute.Colspan,
"2");
htmlWriter.RenderBeginTag(HtmlTextWriterTag.Td);
m_lbl.RenderControl(htmlWriter);
htmlWriter.RenderEndTag();
htmlWriter.RenderEndTag();
break;
default:
Debug.Assert(false);
break;
}
// Close table tag
htmlWriter.RenderEndTag();
// If you wish to make any modifications to the raw Html code
// before it is send to the output stream (for example, ensuring
// that the code is XHtml compliant, etc.), you can make the
// modifications to the 'rawHtml' field below. This code will
// then be sent to the real Html output stream.
string rawHtml = stringBuilder.ToString();
output.Write(rawHtml);
}
/// <summary>
/// Fires when the value of the Checked property changes. This
event only fires when <see cref="P:SupportCheckBox.CallbackEnabled"/> is set
to True.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance
containing the event data.</param>
protected virtual void RaiseCheckedChanged(object sender, EventArgs e)
{
// ToDo: Is there a better way to do this (update properties
automatically)
this.CheckboxChecked = ((CallbackCheckBox)sender).Checked;
// Ensure that the event has a subscriber. If not, don't fire it.
if (CheckedChanged != null)
{
// Raise the CheckedChanged event
CheckedChanged(this, e);
}
}
#endregion
#region Private Methods
/// <summary>
/// Sets the control's default values.
/// </summary>
private void SetDefaultValues()
{
// Set properties to default values
this.CheckboxChecked = false;
this.CheckboxCssClass = SharedConstants.SupportForm_CheckBoxStyle;
this.CheckboxEnabled = true;
this.CheckboxText = string.Empty;
this.CheckboxTextResourceKey = string.Empty;
this.CheckboxVisible = true;
}
/// <summary>
/// Sets the properties of this control when executing in a design
time environment.
/// </summary>
private void SetDesignTimeProperties()
{
// Associate dependent control properties with this control's
properties
m_lbl.CssClass = this.LabelCssClass;
m_lbl.Text = this.LabelText == string.Empty ?
m_lbl.Text = "[LabelText]" :
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_chk.RadControlsDir = this.ScriptsPath;
m_chk.CallbackEnabled = this.CallbackEnabled;
m_chk.DisableAtCallback = this.DisableAtCallback;
m_chk.Enabled = this.CheckboxEnabled;
m_chk.CssClass = this.CheckboxCssClass;
m_chk.Text = this.CheckboxText == string.Empty ?
m_chk.Text = "[CheckboxText]" :
m_chk.Text = this.CheckboxText;
m_chk.Visible = this.CheckboxVisible;
m_chk.Checked = this.CheckboxChecked;
m_chk.AutoPostBack = this.AutoPostback;
m_chk.Enabled = this.Enabled;
m_icn.ImageUrl = 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.WarningIconVisible = this.WarningIconVisible;
m_icn.CallbackEnabled = this.CallbackEnabled;
m_icn.DisableAtCallback = this.DisableAtCallback;
m_icn.Enabled = this.Enabled;
m_icn.CssClass = this.WarningIconCssStyle;
}
/// <summary>
/// Sets the properties of this control when executing in a run time
environment.
/// </summary>
private void SetRunTimeProperties()
{
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_chk.RadControlsDir = this.ScriptsPath;
m_chk.CallbackEnabled = this.CallbackEnabled;
m_chk.DisableAtCallback = this.DisableAtCallback;
m_chk.Enabled = this.CheckboxEnabled;
m_chk.CssClass = this.CheckboxCssClass;
m_chk.Text = this.CheckboxText;
m_chk.Visible = this.CheckboxVisible;
m_chk.Checked = this.CheckboxChecked;
m_chk.AutoPostBack = this.AutoPostback;
m_chk.Enabled = this.Enabled;
m_icn.ImageUrl = 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.WarningIconVisible = this.WarningIconVisible;
m_icn.CallbackEnabled = this.CallbackEnabled;
m_icn.DisableAtCallback = this.DisableAtCallback;
m_icn.Enabled = this.Enabled;
m_icn.CssClass = this.WarningIconCssStyle;
}
#endregion
#region Public Properties
/// <summary>
/// Gets or sets the checkbox CSS class.
/// </summary>
/// <value>The checkbox CSS class.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(string), "")]
[Description("CSS Class name applied to this control.")]
[Localizable(false)]
public string CheckboxCssClass
{
get
{
EnsureChildControls();
return (m_chk.CssClass == null) ?
string.Empty :
m_chk.CssClass;
}
set
{
Debug.Assert(value != null, "Warning: CheckboxCssClass
property is null!");
if (value != null)
{
EnsureChildControls();
m_chk.CssClass = value;
}
else
{
throw new NullReferenceException("CheckboxCssClass can
not be assigned a null value.");
}
}
}
/// <summary>
/// Gets or sets the checkbox text.
/// </summary>
/// <value>The checkbox text.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(string), "")]
[Description("The text that will be displayed with the checkbox.")]
[Localizable(true)]
[NotifyParentProperty(true)]
[Browsable(true)]
[RefreshProperties(RefreshProperties.All)]
public string CheckboxText
{
get
{
EnsureChildControls();
return (m_chk.Text == null) ?
string.Empty :
m_chk.Text;
}
set
{
Debug.Assert(value != null, "Warning: CheckboxText property
is null!");
if (value != null)
{
EnsureChildControls();
m_chk.Text = value;
}
else
{
throw new NullReferenceException("CheckboxText can not
be assigned a null value.");
}
}
}
/// <summary>
/// Gets or sets a value indicating whether the checkbox is enabled.
/// </summary>
/// <value><c>true</c> if checkbox enabled; otherwise,
<c>false</c>.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(bool), "True")]
[Description("Whether or not the Checkbox is enabled.")]
[Localizable(false)]
public bool CheckboxEnabled
{
get
{
EnsureChildControls();
return m_chk.Enabled;
}
set
{
EnsureChildControls();
m_chk.Enabled = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether the checkbox is checked.
/// </summary>
/// <value><c>true</c> if checkbox checked; otherwise,
<c>false</c>.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(bool), "False")]
[Description("Whether or not the Checkbox is selected (checked) or
not.")]
[Localizable(false)]
public bool CheckboxChecked
{
get
{
EnsureChildControls();
return m_chk.Checked;
}
set
{
EnsureChildControls();
m_chk.Checked = value;
}
}
/// <summary>
/// Gets or sets the checkbox text resource key.
/// </summary>
/// <value>The checkbox text resource key.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(string), "")]
[Description("The resource key used when localising the Checkboxes
text.")]
[Localizable(false)]
public string CheckboxTextResourceKey
{
get
{
string s = (string)ViewState["CheckboxTextResourceKey"];
return (s == null) ? String.Empty : s;
}
set
{
Debug.Assert(value != null, "Warning:
CheckboxTextResourceKey property is null!");
if (value != null)
{
ViewState["CheckboxTextResourceKey"] = value;
}
else
{
throw new
NullReferenceException("CheckboxTextResourceKey can not be assigned a null
value.");
}
}
}
/// <summary>
/// Gets or sets a value indicating whether the checkbox is visible
or not.
/// </summary>
/// <value><c>true</c> if checkbox visible; otherwise,
<c>false</c>.</value>
[Bindable(true)]
[Category("Checkbox")]
[DefaultValue(typeof(bool), "True")]
[Description("Whether or not the checkbox is visible.")]
[Localizable(false)]
public bool CheckboxVisible
{
get
{
EnsureChildControls();
return m_chk.Visible;
}
set
{
EnsureChildControls();
m_chk.Visible = value;
}
}
#endregion
}
}