ImageButton.OnPreRender(EventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
判斷影像在渲染前是否被點擊。
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)
參數
範例
以下程式碼範例示範如何覆寫該 OnPreRender 方法,使其在自訂 ImageButton 伺服器控制項中始終顯示細邊框。
Note
以下程式碼範例採用單檔案程式碼模型,若直接複製到程式碼背後的檔案中,可能無法正常運作。 程式碼範例的第一部分必須複製到一個副檔名為 .aspx 的空文字檔中。 第二部分必須在 .cs 檔案(C# 的)或 .vb 的檔案(Visual Basic 的)中。 欲了解更多關於網頁表單程式碼模型的資訊,請參見 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 控制項衍生出自訂類別時使用。