CheckBoxList.OnPreRender(EventArgs) 方法

定义

在客户端上呈现 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)

参数

e
EventArgs

包含事件数据的 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 单个列表项使用当前 AutoPostBackCausesValidationValidationGroup 值进行初始化以呈现。

继承者说明

在派生类中重写 OnPreRender(EventArgs) 时,一定要调用基类的 OnPreRender(EventArgs) 方法,以便已注册的委托对事件进行接收。

适用于

另请参阅