Création d'un Custom WebControl
Par ASP.NET - Lien permanent
Depuis que je développe en .NET, j'entend toujours des gens expliquer qu'asp.net génère du code HTML de très mauvaise qualité, etc. Il n'en est rien dans la pratique. En effet, il est tout à fait possible de travailler en ASP.NET et de générer du beau code HTML. J'ai décidé suite à certaines implémentations réalisées il n'y a pas si longtemps ... de réaliser quelques articles sur ce sujet (en effet, nombre d'entre nous ne connait pas toute la puissance d'ASP.NET). Le premier porte ici sur la création d'un contrôle simple affichant une textbox. Côté .NET, nous pouvons récupérer la valeur. J'expliquerai prochainement comment ajouter des validations client JS propres ainsi que la réalisation de contrôles plus complexes.
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>
