public
Last active

ASP.Net Button control that renders with Button tag instead of input

  • Download Gist
ModernButton.cs
C#
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
[ParseChildren(false)]
public class ModernButton : WebControl
{
//Fields
private static readonly object EventClick;
private static readonly object EventCommand;
 
//Events
public event EventHandler Click;
public event CommandEventHandler Command;
 
//Methods
static ModernButton()
{
EventClick = new object();
EventCommand = new object();
}
 
public ModernButton()
: base(HtmlTextWriterTag.Button)
{
}
 
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
PostBackOptions postBackOptions = this.GetPostBackOptions();
string uniqueID = this.UniqueID;
if ((uniqueID != null) && ((postBackOptions == null) || (postBackOptions.TargetControl == this))) {
writer.AddAttribute(HtmlTextWriterAttribute.Name, uniqueID);
}
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.Value);
bool isEnabled = base.IsEnabled;
string firstScript = String.Empty;
if (isEnabled) {
firstScript = EnsureEndWithSemiColon(this.OnClientClick);
if (base.HasAttributes) {
string baseClick = base.Attributes["onclick"];
if (baseClick != null) {
firstScript += EnsureEndWithSemiColon(baseClick);
base.Attributes.Remove("onclick");
}
}
if (this.Page != null) {
string postBackEventReference = this.Page.ClientScript.GetPostBackEventReference(postBackOptions, false);
if (postBackEventReference != null) {
firstScript = MergeScript(firstScript, postBackEventReference);
}
}
}
if (this.Page != null) {
this.Page.ClientScript.RegisterForEventValidation(postBackOptions);
}
if (firstScript.Length > 0) {
writer.AddAttribute(HtmlTextWriterAttribute.Onclick, firstScript);
}
if (this.Enabled && !isEnabled) writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
base.AddAttributesToRender(writer);
}
 
protected override void AddParsedSubObject(object obj)
{
if (this.HasControls()) {
base.AddParsedSubObject(obj);
}
else if (obj is LiteralControl) {
this.InnerHtml = ((LiteralControl) obj).Text;
}
else {
string html = this.InnerHtml;
if (html.Length != 0) {
this.InnerHtml = String.Empty;
base.AddParsedSubObject(new LiteralControl(html));
}
}
}
 
private string EnsureEndWithSemiColon(string value)
{
if (value != null) {
int length = value.Length;
if ((length > 0) && (value[length - 1] != ';')) {
return (value + ';');
}
}
return value;
}
 
protected virtual PostBackOptions GetPostBackOptions()
{
PostBackOptions options = new PostBackOptions(this, String.Empty);
options.ClientSubmit = false;
if (this.Page != null) {
if (this.CausesValidation && (this.Page.GetValidators(this.ValidationGroup).Count > 0)) {
options.PerformValidation = true;
options.ValidationGroup = this.ValidationGroup;
}
if (!String.IsNullOrEmpty(this.PostBackUrl)) {
options.ActionUrl = HttpUtility.UrlPathEncode(base.ResolveClientUrl(this.PostBackUrl));
}
}
return options;
}
 
private string MergeScript(string firstScript, string secondScript)
{
if (!string.IsNullOrEmpty(firstScript)) return (firstScript + secondScript);
if (secondScript.TrimStart(new char[0]).StartsWith("javascript:", StringComparison.Ordinal)) {
return secondScript;
}
return ("javascript:" + secondScript);
}
 
protected void OnClick(EventArgs e)
{
EventHandler handler = (EventHandler) base.Events[EventClick];
if (handler != null) handler(this, e);
}
 
protected void OnCommand(CommandEventArgs e)
{
CommandEventHandler handler = (CommandEventHandler) base.Events[EventCommand];
if (handler != null) handler(this, e);
base.RaiseBubbleEvent(this, e);
}
 
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write(this.InnerHtml);
}
 
//Properties
public virtual bool CausesValidation
{
get
{
object causesValidation = this.ViewState["CausesValidation"];
if (causesValidation != null) return (bool) causesValidation;
return true;
}
set
{
this.ViewState["CausesValidation"] = value;
}
}
 
public string CommandArgument
{
get
{
return this.ViewState["CommandArgument"] as string ?? String.Empty;
}
set
{
this.ViewState["CommandArgument"] = value;
}
}
 
public string CommandName
{
get
{
return this.ViewState["CommandName"] as string ?? String.Empty;
}
set
{
this.ViewState["CommandName"] = value;
}
}
 
public string InnerHtml
{
get
{
return this.ViewState["InnerHtml"] as string ?? String.Empty;
}
set
{
this.ViewState["InnerHtml"] = value;
}
}
 
public virtual string OnClientClick
{
get
{
return this.ViewState["OnClientClick"] as string ?? String.Empty;
}
set
{
this.ViewState["OnClientClick"] = value;
}
}
 
public virtual string PostBackUrl
{
get
{
return this.ViewState["PostBackUrl"] as string ?? String.Empty;
}
set
{
this.ViewState["PostBackUrl"] = value;
}
}
 
public virtual string ValidationGroup
{
get
{
return this.ViewState["ValidationGroup"] as string ?? String.Empty;
}
set
{
this.ViewState["ValidationGroup"] = value;
}
}
 
public string Value
{
get
{
return this.ViewState["Value"] as string ?? String.Empty;
}
set { this.ViewState["Value"] = value; }
}
}

Hi,

You forgot to implement the interface IPostBackEventHandler so you can fire the events on postback:

   public void RaisePostBackEvent(string eventArgument) {
      if (CausesValidation)
        Page.Validate(ValidationGroup);
      OnClick(EventArgs.Empty);
      if (!string.IsNullOrEmpty(CommandName))
        this.OnCommand(new CommandEventArgs(this.CommandName, this.CommandArgument));
    }

You also forgot to store the eventhandlers on the events collection:

    public event EventHandler Click {
      add { Events.AddHandler(EventClick, value); }
      remove { Events.RemoveHandler(EventClick, value); }
    }
    public event CommandEventHandler Command {
      add { Events.AddHandler(EventCommand, value); }
      remove { Events.RemoveHandler(EventCommand, value); }
    }

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.