ValidationSummary.Render(HtmlTextWriter) 方法

定义

将服务器控件内容发送到给定的 HtmlTextWriter 对象,该对象可编写将在客户端呈现的内容。

protected:
 override void Render(System::Web::UI::HtmlTextWriter ^ writer);
protected public:
 override void Render(System::Web::UI::HtmlTextWriter ^ writer);
protected override void Render (System.Web.UI.HtmlTextWriter writer);
protected internal override void Render (System.Web.UI.HtmlTextWriter writer);
override this.Render : System.Web.UI.HtmlTextWriter -> unit
Protected Overrides Sub Render (writer As HtmlTextWriter)
Protected Friend Overrides Sub Render (writer As HtmlTextWriter)

参数

writer
HtmlTextWriter

在客户端上呈现 HTML 内容的输出流。

示例

下面的代码示例演示如何重写 Render 自定义服务器控件中的 方法,以便控件 ValidationSummarySmall 标记包围。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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 - Render - C# Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom ValidationSummary - Render - 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:CustomValidationSummaryRender
              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 - Render - VB.NET Example</title>
    </head>
    <body>
        <form id="Form1" method="post" runat="server">
            <h3>Custom ValidationSummary - Render - 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>
                </tr>
                <tr>
                    <td valign="top">
                        <aspSample:CustomValidationSummaryRender
                         id="ValidationSummary1" runat="server" DisplayMode="BulletList" 
                         HeaderText="You must enter a value in the following fields:" />
                    </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 CustomValidationSummaryRender : System.Web.UI.WebControls.ValidationSummary
  {
    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
      // Write out begining Small HTML tag.
      writer.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Small);

      // Call the base class's Render method.
      base.Render(writer);

      // Write out ending Small HTML tag.
      writer.RenderEndTag();
    }
  }
}
Imports System.Web
Imports System.Security.Permissions

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

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

            ' Write out begining Small HTML tag.
            writer.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Small)

            ' Call the base class's Render method.
            MyBase.Render(writer)

            ' Write out ending Small HTML tag.
            writer.RenderEndTag()
        End Sub
    End Class
End Namespace

适用于