ValidationSummary.OnPreRender(EventArgs) 方法

定義

引發 PreRender 事件。Raises the PreRender event.

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

包含事件資料的 EventArgsAn EventArgs that contains the event data.

範例

下列程式碼範例示範如何覆寫 OnPreRender 方法,以便在自訂伺服器控制項中將錯誤訊息一律顯示為粗體 ValidationSummaryThe following code example demonstrates how to override the OnPreRender method so that it always displays error messages as bold in a custom ValidationSummary server control.

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。This example has a text box that accepts user input, which is a potential security threat. 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。By default, ASP.NET Web pages validate that user input does not include script or HTML elements. 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。For more information, see Script Exploits Overview.

<%@ 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 ValidationSummary - AddAttributesToRender - C# Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom ValidationSummary - AddAttributesToRender - C# Example</h3>

      <table id="Table1" cellpadding="4" cellspacing="0">
        <tr>
          <td>
            <table id="Table2" cellpadding="4" cellspacing="0"
             style="background-color:#eeeeee;">
              <tr>
                <td colspan="3"><b>Credit Card Information</b> </td>
              </tr>
              <tr>
                <td align="right">Card Type: </td>
                <td>
                  <asp:RadioButtonList id="RadioButtonList1" runat="server" RepeatLayout="Flow">
                      <asp:ListItem>MasterCard</asp:ListItem>
                      <asp:ListItem>Visa</asp:ListItem>
                  </asp:RadioButtonList>
                </td>
                <td align="center" rowspan="1">
                  <asp:RequiredFieldValidator 
                    id="RequiredFieldValidator1" 
                    runat="server" 
                    ErrorMessage="Card Type" 
                    Width="100%" 
                    Display="Static" 
                    ControlToValidate="RadioButtonList1">*
                  </asp:RequiredFieldValidator>
                </td>
              </tr>
              <tr>
                <td align="right">Card Number: </td>
                <td><asp:TextBox id="TextBox1" runat="server"></asp:TextBox></td>
                <td>
                  <asp:RequiredFieldValidator 
                    id="RequiredFieldValidator2" 
                    runat="server" 
                    ErrorMessage="Card Number" 
                    Width="100%" 
                    Display="Static" 
                    ControlToValidate="TextBox1">*
                  </asp:RequiredFieldValidator>
                </td>
              </tr>
              <tr>
                <td></td>
                <td><asp:Button id="Button1" runat="server" text="Validate"></asp:Button></td>
                <td></td>
              </tr>
            </table>
          </td>
        </tr>
        <tr>
          <td valign="top">
            <aspSample:CustomValidationSummaryAddAttributesToRender
              id="ValidationSummary1" 
              runat="server" 
              DisplayMode="BulletList" 
              HeaderText="You must enter a value in the following fields:" />
          </td>
        </tr>
      </table>

        </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 ValidationSummary - AddAttributesToRender - VB.NET Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom ValidationSummary - AddAttributesToRender - VB.NET Example</h3>


                <table id="Table1" cellpadding="4" cellspacing="0">
                    <tr>
                        <td>
                            <table id="Table2" cellpadding="4" cellspacing="0" style="background-color:#eeeeee">
                                <tr>
                                    <td colspan="3"><b>Credit Card Information</b> </td>
                                </tr>
                                <tr>
                                    <td align="right">Card Type: </td>
                                    <td><asp:RadioButtonList id="RadioButtonList1" runat="server" RepeatLayout="Flow">
                                            <asp:ListItem>MasterCard</asp:ListItem>
                                            <asp:ListItem>Visa</asp:ListItem>
                                        </asp:RadioButtonList>
                                    </td>
                                    <td align="center" rowspan="1">
                                        <asp:RequiredFieldValidator id="RequiredFieldValidator1" 
                                         runat="server" ErrorMessage="Card Type. " Width="100%" 
                                         Display="Static" ControlToValidate="RadioButtonList1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">Card Number: </td>
                                    <td><asp:TextBox id="TextBox1" runat="server"></asp:TextBox></td>
                                    <td>
                                        <asp:RequiredFieldValidator id="RequiredFieldValidator2" 
                                         runat="server" ErrorMessage="Card Number. " Width="100%" 
                                         Display="Static" ControlToValidate="TextBox1">*</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td><asp:Button id="Button1" runat="server" text="Validate"></asp:Button></td>
                                    <td></td>
                                </tr>
                            </table>
                        </td>
                        <td valign="top">
                            <table id="Table3" cellpadding="4" cellspacing="0" >
                                <tr>
                                    <td><aspSample:CustomValidationSummaryAddAttributesToRender
                                        id="ValidationSummary1" runat="server" DisplayMode="BulletList"
                                        HeaderText="You must enter a value in the following fields:" />
                                    </td>
                                </tr>
                            </table>
                        </td>
                     </tr>
                </table>

        </form>
    </body>
</html>
using System.Web;
using System.Security.Permissions;

namespace Samples.AspNet.CS.Controls
{
  [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
  public sealed class CustomValidationSummaryAddAttributesToRender : System.Web.UI.WebControls.ValidationSummary
  {
    protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
    {
      // Show the error message as bold.
      writer.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontWeight, "bold");
      
      // Call the Base's AddAttributesToRender method.
      base.AddAttributesToRender(writer);
    }
  }
}
Imports System.Web
Imports System.Security.Permissions

Namespace Samples.AspNet.VB.Controls
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public NotInheritable Class CustomValidationSummaryAddAttributesToRender
        Inherits System.Web.UI.WebControls.ValidationSummary

        Protected Overrides Sub AddAttributesToRender(ByVal writer As System.Web.UI.HtmlTextWriter)

            ' Show the error message as bold.
            writer.AddStyleAttribute(System.Web.UI.HtmlTextWriterStyle.FontWeight, "bold")

            ' Call the Base's AddAttributesToRender method.
            MyBase.AddAttributesToRender(writer)
        End Sub
    End Class
End Namespace

適用於