Редагувати

Поділитися через


Page.RegisterRequiresPostBack(Control) Method

Definition

Registers a control as one that requires postback handling when the page is posted back to the server.

public:
 void RegisterRequiresPostBack(System::Web::UI::Control ^ control);
public void RegisterRequiresPostBack (System.Web.UI.Control control);
member this.RegisterRequiresPostBack : System.Web.UI.Control -> unit
Public Sub RegisterRequiresPostBack (control As Control)

Parameters

control
Control

The control to be registered.

Exceptions

The control to register does not implement the IPostBackDataHandler interface.

Examples

The following code example uses the RegisterRequiresPostBack method to require a text box control, myTextBox, to be posted back before any code associated with the text box can execute. TextBox controls implement the IPostBackDataHandler interface.

Important

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

protected void Text_Change(object sender, EventArgs e)
{
  myLabel.Text = "<b>Welcome " + myTextBox.Text + " to ASP.NET</b>";
}

protected void Page_PreRender(object sender, EventArgs e)
{
  this.RegisterRequiresPostBack(myTextBox);
}
Sub Text_Change(ByVal sender As Object, ByVal e As EventArgs)
  myLabel.Text = "<b>Welcome " + myTextBox.Text + " to ASP.NET</b>"
End Sub 'Text_Change

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs)
  Me.RegisterRequiresPostBack(myTextBox)
End Sub

Remarks

The control to be registered must implement the IPostBackDataHandler interface or an HttpException is raised. When implemented by a control, the IPostBackDataHandler interface enables handling of post back data and raising of any post back data changed events. For more information on the server control event model, see ASP.NET Web Forms Server Control Event Model.

Register controls with the page at or before the Page_PreRender event of the page life cycle.

Applies to

See also