ImageButton.OnPreRender(EventArgs) 方法

定义

确定图像在客户端上呈现之前是否已被单击。

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 对象。

示例

下面的代码示例演示如何重写 OnPreRender 方法,以便始终在自定义 ImageButton 服务器控件中显示精简边框。

备注

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 代码示例的第一部分必须复制到具有 .aspx 扩展名的空文本文件中。 第二部分必须位于 C#) 的 .cs 文件 (中,或者Visual Basic) 的 .vb 文件 (。 有关Web Forms代码模型的详细信息,请参阅 ASP.NET Web Forms页代码模型

<%@ 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 ImageButton - OnPreRender - C# Example</title>
    <script runat="server">
      void ImageButton1_Command(Object sender, CommandEventArgs e) 
      {
        // Redirect to the Microsoft home page.
        Response.Redirect("http://www.microsoft.com/");
      }
    </script>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom ImageButton - OnPreRender - C# Example</h3>
            
            <aspSample:CustomImageButtonOnPreRender 
              id="ImageButton1" 
              runat="server" 
              OnCommand="ImageButton1_Command" 
              AlternateText="Microsoft Home" 
              ImageUrl="http://www.microsoft.com/homepage/gif/bnr-microsoft.gif" />

        </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 ImageButton - OnPreRender - VB.NET Example</title>
        <script runat="server">
            Sub ImageButton1_Command(sender As Object, e As CommandEventArgs)
                ' Redirect to the Microsoft home page.
                Response.Redirect("http://www.microsoft.com/")
            End Sub
        </script>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom ImageButton - OnPreRender - VB.NET Example</h3>
            
            <aspSample:CustomImageButtonOnPreRender id="ImageButton1" runat="server" 
             OnCommand="ImageButton1_Command" AlternateText="Microsoft Home" 
             ImageUrl="http://www.microsoft.com/homepage/gif/bnr-microsoft.gif" />

        </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 CustomImageButtonOnPreRender : ImageButton
    {
        protected override void OnPreRender(EventArgs e)
        {
            // Run the OnPreRender method on the base class.
            base.OnPreRender(e);

            // Always display the ImageButton with a thin border.
            this.BorderWidth =  Unit.Point(1);
        }
    }
}
<AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class CustomImageButtonOnPreRender
    Inherits ImageButton

    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)

        ' Run the OnPreRender method on the base class.
        MyBase.OnPreRender(e)

        ' Always display the ImageButton with a thin border.
        Me.BorderWidth = Unit.Point(1)
    End Sub
End Class

注解

从控件派生自定义类ImageButton时,该方法OnPreRender主要由控件开发人员使用。

适用于

另请参阅