共用方式為


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 網頁代碼模型

<%@ 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

備註

方法 OnPreRender 主要是由控件開發人員從 ImageButton 控件衍生自定義類別時使用。

適用於

另請參閱