How can I use the button tag with ASP.NET?

前端 未结 7 1510
天命终不由人
天命终不由人 2020-11-28 03:33

I\'d like to use the newer

7条回答
  •  孤街浪徒
    2020-11-28 03:57

    This is an old question, but for those of us unlucky enough still having to maintain ASP.NET Web Forms applications, I went through this myself while trying to include Bootstrap glyphs inside of built-in button controls.

    As per Bootstrap documentation, the desired markup is as follows:

    
    

    I needed this markup to be rendered by a server control, so I set out to find options.

    Button

    This would be the first logical step, but —as this question explains— Button renders an element instead of

  • Renders and relies on obtrusive JavaScript

HtmlButton (credit to Philippe's answer)

Source


Result


Pros

  • Looks OK
  • Renders proper

Cons

  • No Command event; no CommandName or CommandArgument properties
  • Renders and relies on obtrusive JavaScript to handle its ServerClick event

At this point it is clear that none of the built-in controls seem suitable, so the next logical step is try and modify them to achieve the desired functionality.

Custom control (credit to Dan Herbert's answer)

NOTE: This is based on Dan's code, so all credit goes to him.

using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModernControls
{
    [ParseChildren]
    public class ModernButton : Button
    {
        public new string Text
        {
            get { return (string)ViewState["NewText"] ?? ""; }
            set { ViewState["NewText"] = value; }
        }

        public string Value
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Button; }
        }

        protected override void AddParsedSubObject(object obj)
        {
            var literal = obj as LiteralControl;
            if (literal == null) return;
            Text = literal.Text;
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write(Text);
        }
    }
}

I have stripped the class down to the bare minimum, and refactored it to achieve the same functionality with as little code as possible. I also added a couple of improvements. Namely:

  • Remove PersistChildren attribute (seems unnecessary)
  • Remove TagName override (seems unnecessary)
  • Remove HTML decoding from Text (base class already handles this)
  • Leave OnPreRender intact; override AddParsedSubObject instead (simpler)
  • Simplify RenderContents override
  • Add a Value property (see below)
  • Add a namespace (to include a sample of @ Register directive)
  • Add necessary using directives

The Value property simply accesses the old Text property. This is because the native Button control renders a value attribute anyway (with Text as its value). Since value is a valid attribute of the

Pros

  • Looks OK
  • Renders a proper
  • Command event; CommandName and CommandArgument properties
  • Does not render or rely on obtrusive JavaScript

Cons

  • None (other than not being a built-in control)

提交回复
热议问题