Page.VerifyRenderingInServerForm(Control) 方法

定义

确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件。Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.

public:
 virtual void VerifyRenderingInServerForm(System::Web::UI::Control ^ control);
public virtual void VerifyRenderingInServerForm (System.Web.UI.Control control);
abstract member VerifyRenderingInServerForm : System.Web.UI.Control -> unit
override this.VerifyRenderingInServerForm : System.Web.UI.Control -> unit
Public Overridable Sub VerifyRenderingInServerForm (control As Control)

参数

control
Control

必须位于 HtmlForm 控件中的 ASP.NET 服务器控件。The ASP.NET server control that is required in the HtmlForm control.

例外

指定的服务器控件在运行时不包含在 HtmlForm 服务器控件的开始和结束标记之间。The specified server control is not contained between the opening and closing tags of the HtmlForm server control at run time.

要验证的控件为 nullThe control to verify is null.

示例

下面的代码示例将重写 Page.Render 自定义服务器控件的方法。The following code example overrides the Page.Render method of a custom server control. 此控件将其内容写入页面后,将使用 VerifyRenderingInServerForm 方法确保控件出现在控件的开始标记和结束标记之间 HtmlFormWhen this control writes its content to a page, it uses the VerifyRenderingInServerForm method to make sure that the control appears between the opening and closing tags of an HtmlForm control.

// Override the Render method to ensure that this control
// is nested in an HtmlForm server control, between a <form runat=server>
// opening tag and a </form> closing tag.
protected override void Render(HtmlTextWriter writer) {
    // Ensure that the control is nested in a server form.
    if (Page != null) {
        Page.VerifyRenderingInServerForm(this);
    }
    base.Render(writer);
}
' Override the Render method to ensure that this control
' is nested in an HtmlForm server control, between a <form runat=server>
' opening tag and a </form> closing tag.
Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

    ' Ensure that the control is nested in a server form.
    If Not (Page Is Nothing) Then
        Page.VerifyRenderingInServerForm(Me)
    End If

    MyBase.Render(writer)

End Sub

注解

需要位于标记内的控件 <form runat=server> 可以在呈现之前调用此方法,以便在将错误消息放置在标记之外时显示错误消息。Controls that are required to be inside <form runat=server> tags can call this method before they render so that an error message is shown if they are placed outside the tags. 回发或依赖于已注册脚本块的控件应在方法的重写中调用此方法 Control.RenderControls that post back or depend on registered script blocks should call this method in an override of the Control.Render method. 具有不同的呈现服务器窗体元素方式的页可以重写此方法,以在不同条件下引发异常。Pages that have a different way of rendering the server form element can override this method to throw an exception under different conditions.

如果回发或使用客户端脚本的服务器控件未包含在 HtmlForm 服务器控件中 ( # B0) 标记,则它们将无法工作 form runat="server">Server controls that post back or use client-side script will not work if they are not enclosed in the HtmlForm server control (<form runat="server">) tags. 这些控件可以在呈现时调用此方法,以便在控件未包含时提供清晰的错误消息 HtmlFormThese controls can call this method when they render to provide a clear error message when they are not enclosed in the HtmlForm control.

开发自定义服务器控件时,在 Render 为任何类型的输入标记重写方法时,通常会调用此方法。When you develop a custom server control, it is common to call this method when you override the Render method for any kind of input tag. 如果输入控件调用 GetPostBackEventReference 或发出客户端脚本,则这一点特别重要。This is particularly important if the input control calls GetPostBackEventReference, or if it emits client script. 复合服务器控件无需进行此调用。A composite server control does not need to make this call.

适用于