Button.AddAttributesToRender(HtmlTextWriter) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将 Button 控件的特性添加到输出流用以在客户端上呈现内容。
protected:
override void AddAttributesToRender(System::Web::UI::HtmlTextWriter ^ writer);
protected override void AddAttributesToRender (System.Web.UI.HtmlTextWriter writer);
override this.AddAttributesToRender : System.Web.UI.HtmlTextWriter -> unit
Protected Overrides Sub AddAttributesToRender (writer As HtmlTextWriter)
参数
- writer
- HtmlTextWriter
一个 HtmlTextWriter,其中包含要在客户端上呈现内容的输出流。
示例
下面的代码示例演示如何替代 AddAttributesToRender 自定义服务器控件中的 方法,以便 Button 文本始终以红色显示。
<%@ Page language="c#" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Custom Button - AddAttributesToRender - C# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom Button - AddAttributesToRender - C# Example</h3>
<aspSample:CustomButtonAddAttributesToRender
id="Button1"
runat="server"
Text="Button" />
</form>
</body>
</html>
<%@ Page language="VB" %>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Custom Button - AddAttributesToRender - VB.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom Button - AddAttributesToRender - VB.NET Example</h3>
<aspSample:CustomButtonAddAttributesToRender id="Button1" runat="server" Text="Button" />
</form>
</body>
</html>
using System.Web;
using System.Security.Permissions;
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
public sealed class CustomButtonAddAttributesToRender : System.Web.UI.WebControls.Button
{
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
// Add a client-side onclick event to the button.
writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Onclick, "alert('Hello World');");
// Update the text of the button to be shown in the color Red
writer.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Color, "Red");
// Call the base's AddAttributesToRender method
base.AddAttributesToRender(writer);
}
}
}
Imports System.Web
Imports System.Security.Permissions
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public NotInheritable Class CustomButtonAddAttributesToRender
Inherits System.Web.UI.WebControls.Button
Protected Overrides Sub AddAttributesToRender(ByVal writer As System.Web.UI.HtmlTextWriter)
' Add a client-side onclick event to the button.
writer.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Onclick, "alert('Hello World');")
' Update the text of the button to be shown in the color Red
writer.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.Color, "Red")
' Call the base's AddAttributesToRender method
MyBase.AddAttributesToRender(writer)
End Sub
End Class
End Namespace ' Samples.AspNet