TextBox.AddAttributesToRender(HtmlTextWriter) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
新增需要渲染到指定 HtmlTextWriter 實例的 HTML 屬性與樣式。
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 代表輸出串流,用來在客戶端渲染 HTML 內容。
範例
以下程式碼範例示範如何在自訂伺服器控制項中覆寫此 AddAttributesToRender 方法,使 TextBox 控制文字始終顯示為粗體。
Important
此範例中有一個文字框可接受使用者輸入,這可能構成安全威脅。 預設情況下,ASP.NET 網頁會驗證使用者輸入中不包含腳本或 HTML 元素。 欲了解更多資訊,請參閱 腳本漏洞概述。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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 TextBox - AddAttributesToRender - C# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom TextBox - AddAttributesToRender - C# Example</h3>
<aspSample:CustomTextBoxAddAttributesToRender
id="TextBox1"
runat="server">Hello World!
</aspSample:CustomTextBoxAddAttributesToRender>
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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 TextBox - AddAttributesToRender - VB.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom TextBox - AddAttributesToRender - VB.NET Example</h3>
<aspSample:CustomTextBoxAddAttributesToRender id="TextBox1"
runat="server">Hello World!</aspSample:CustomTextBoxAddAttributesToRender>
</form>
</body>
</html>
using System.Web;
using System.Security.Permissions;
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
public sealed class CustomTextBoxAddAttributesToRender : System.Web.UI.WebControls.TextBox
{
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
// Show the TextBox text as Bold.
writer.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontWeight, "bold");
// Call the base 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 CustomTextBoxAddAttributesToRender
Inherits System.Web.UI.WebControls.TextBox
Protected Overrides Sub AddAttributesToRender(ByVal writer As System.Web.UI.HtmlTextWriter)
' Show the TextBox text as Bold.
writer.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontWeight, "bold")
' Call the base AddAttributesToRender method.
MyBase.AddAttributesToRender(writer)
End Sub
End Class
End Namespace
備註
此方法主要由控制項開發者用來在控制項的輸出串流HtmlTextWriter中插入額外的屬性與樣式TextBox。 這個方法會覆寫 WebControl.AddAttributesToRender。