CheckBoxList.OnPreRender(EventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在用戶端上呈現以前,先設定 CheckBoxList 控制項。
protected:
override void OnPreRender(EventArgs ^ e);
protected public:
override void OnPreRender(EventArgs ^ e);
protected override void OnPreRender (EventArgs e);
protected internal override void OnPreRender (EventArgs e);
override this.OnPreRender : EventArgs -> unit
Protected Overrides Sub OnPreRender (e As EventArgs)
Protected Friend Overrides Sub OnPreRender (e As EventArgs)
參數
範例
下列程式碼範例示範如何在自訂伺服器控制項中覆寫 CreateControlStyle 方法,以便一律傳 CheckBoxList 回 類別的新實例 TableStyle 。
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS" %>
<%@ Page language="c#" %>
<!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 CheckBoxList - OnPreRender - C# Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom CheckBoxList - OnPreRender - C# Example</h3>
<aspSample:CustomCheckBoxListOnPreRender
id="CheckBoxList1" runat="server">
<asp:ListItem Selected="True">Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</aspSample:CustomCheckBoxListOnPreRender>
</form>
</body>
</html>
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB" %>
<%@ Page language="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 CheckBoxList - OnPreRender - VB.NET Example</title>
</head>
<body>
<form id="Form1" method="post" runat="server">
<h3>Custom CheckBoxList - OnPreRender - VB.NET Example</h3>
<aspSample:CustomCheckBoxListOnPreRender id="CheckBoxList" runat="server"
RepeatLayout="Table" RepeatColumns="2" CellSpacing="3" CellPadding="3">
<asp:ListItem Selected="True">Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</aspSample:CustomCheckBoxListOnPreRender>
</form>
</body>
</html>
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Security.Permissions;
namespace Samples.AspNet.CS.Controls
{
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
public class CustomCheckBoxListOnPreRender : CheckBoxList
{
protected override void OnPreRender(EventArgs e)
{
// Run the OnPreRender method on the base class.
base.OnPreRender(e);
// Display the Calendar with a 3 point border.
this.BorderWidth = Unit.Point(3);
}
}
}
Imports System.Web
IMports System.Web.UI.WebControls
Imports System.Security.Permissions
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class CustomCheckBoxListOnPreRender
Inherits System.Web.UI.WebControls.CheckBoxList
Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
' Run the OnPreRender method on the base class.
MyBase.OnPreRender(e)
' Display the CheckBoxList with a 3 point border.
Me.BorderWidth = Unit.Point(3)
End Sub
End Class
End Namespace
備註
方法 OnPreRender 主要是由衍生自 類別的 CheckBoxList 自訂控制項中的控制項開發人員使用。 方法 OnPreRender 可讓衍生類別處理事件, PreRender 而不附加委派。 這是在衍生類別中處理事件的慣用技巧。
方法 OnPreRender 會在儲存檢視狀態和轉 CheckBoxList 譯內容之前,先執行任何必要的預先呈現步驟。 控制項中包含的 CheckBoxList 個別清單專案會初始化,以使用目前的 AutoPostBack 、 CausesValidation 和 ValidationGroup 值來轉譯。
給繼承者的注意事項
當在衍生類別中覆寫 OnPreRender(EventArgs) 時,請確定呼叫基底類別的 OnPreRender(EventArgs) 方法,使已註冊的委派能接收到事件。