ASP.NET Button and overriding the render event
Firstly could someone please be kind enough to comment on why Microsoft might have chosen to make the ASP.NET button control render an <input type='button' />
tag rather than a <button />
tag? I have seen a number of issues (particularly when creating jQuery buttons with no text and just icons) as a result of this.
Secondly I am attempting to create my own custom button output by overriding the Render()
event and am having a bit of difficulty trying to understand how to go about changing the output that is provided by this event.
If I look at the HtmlTextWriter
that is provided as the parameter to the Render()
event I can see it contains a protected property TagKey = Input {47}
, is it possible to simply modify this property somehow and change it to a Button or do I need to create a new instance of the HtmlTextWriter and populate it all from the start? If so, could anyone provide some guidance as to how this would typically be done and if there are any special considerations I need to make to ensure that my derived button class is functio开发者_如何学Gonally equivalent to the original (excluding the html)?
<Assembly: TagPrefix("MyCompany", "MyCustomButton")>
Public Class MyButton
Inherits Button
Protected Overrides Sub OnPreRender(e As System.EventArgs)
MyBase.OnPreRender(e)
End Sub
Protected Overrides Sub Render(writer As System.Web.UI.HtmlTextWriter)
'writer.TagKey = Input {47}
MyBase.Render(writer)
End Sub
End Class
No idea on Microsoft's decision, but for the TagKey
, you just need to override the TagKey
property and return HtmlTextWriterTag.Button
. You will also want to override the AddAttributesToRender
method and not call the base method so that it wouldn't output the type="button"
attribute.
The Button
element has some quirks when used in different browsers, particularly regarding the data that is submitted. Microsoft most likely chose to use the input for greater consistency between browser implementations.
精彩评论