Page.VerifyRenderingInServerForm(Control) Page.VerifyRenderingInServerForm(Control) Page.VerifyRenderingInServerForm(Control) Method

定義

確認在 Run Time 時期呈現了所指定 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

參數

control
Control Control Control

HtmlForm 控制項中的必要 ASP.NET 伺服器控制項。The ASP.NET server control that is required in the HtmlForm control.

例外狀況

指定的伺服器控制項在 Run Time 時期並不包含於 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方法來確定控制項的開頭和結尾HtmlForm標籤之間會顯示該控制項會。When 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.Render方法的覆寫中呼叫這個方法。Controls 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伺服器控制項(<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. 這些控制項可以在轉譯時呼叫這個方法,以在HtmlForm控制項未括住時提供清楚的錯誤訊息。These 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.

適用於