La première étape est de créer une nouvelle classe héritant de WebControl. Nous décidons de l'appeler PercentageControl.
[ToolboxData("<{0}:PercentageControl runat=server></{0}:PercentageControl>")]
public class PercentageControl : WebControl
{
}
[ToolboxData("<{0}:PercentageControl runat=server></{0}:PercentageControl>")]
public class PercentageControl : WebControl
{
[Bindable(true)]
[Category("Custom Category")]
[DefaultValue("")]
[Description("The percentage value.")]
[Localizable(false)]
public double? Value
{
get
{
var value = this.ViewState["Value"];
if (value != null)
{
return (double)value;
}
return default(double?);
}
set { this.ViewState["Value"] = value; }
}
}
{
get { return HtmlTextWriterTag.Input; }
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Value.HasValue
? this.Value.ToString()
: string.Empty);
}
{
base.OnPreRender(e);
this.Page.RegisterRequiresPostBack(this);
}
#region IPostBackDataHandler Members
{
var result = postCollection[this.UniqueID];
if (string.IsNullOrEmpty(result))
{
this.Value = default(double?);
return !this.Value.HasValue;
}
double newValue;
if (double.TryParse(result, out newValue))
{
bool hasChanged = this.Value != newValue;
this.Value = newValue;
return hasChanged;
}
return false;
public void RaisePostDataChangedEvent()
{
}
[ToolboxData("<{0}:PercentageControl runat=server></{0}:PercentageControl>")]
[ToolboxBitmap(typeof(PercentageControl), "Percentage.bmp")]
public class PercentageControl : WebControl, IPostBackDataHandler
{
[Bindable(true)]
[Category("Custom Category")]
[DefaultValue("")]
[Description("The percentage value.")]
[Localizable(false)]
public double? Value
{
get
{
var value = this.ViewState["Value"];
if (value != null)
{
return (double)value;
}
return default(double?);
}
set { this.ViewState["Value"] = value; }
}
protected override HtmlTextWriterTag TagKey
{
get { return HtmlTextWriterTag.Input; }
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Value.HasValue
? this.Value.ToString()
: string.Empty);
}
protected override void OnPreRender(System.EventArgs e)
{
base.OnPreRender(e);
this.Page.RegisterRequiresPostBack(this);
}
#region IPostBackDataHandler Members
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
var result = postCollection[this.UniqueID];
if (string.IsNullOrEmpty(result))
{
this.Value = default(double?);
return !this.Value.HasValue;
}
double newValue;
if (double.TryParse(result, out newValue))
{
bool hasChanged = this.Value != newValue;
this.Value = newValue;
return hasChanged;
}
return false;
}
public void RaisePostDataChangedEvent()
{
}
#endregion
}
<Custom:PercentageControl runat="server" Value="200" />
<asp:Button Text="Click" runat="server" />
</div>