Export (0) Print
Expand All

PostBackOptions Class

Specifies how client-side JavaScript is generated to initiate a postback event.

System.Object
  System.Web.UI.PostBackOptions

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public sealed class PostBackOptions

The PostBackOptions type exposes the following members.

  NameDescription
Public methodPostBackOptions(Control)Initializes a new instance of the PostBackOptions class with the specified target control data.
Public methodPostBackOptions(Control, String)Initializes a new instance of the PostBackOptions class with the specified target control and argument data.
Public methodPostBackOptions(Control, String, String, Boolean, Boolean, Boolean, Boolean, Boolean, String)Initializes a new instance of the PostBackOptions class with the specified values for the instance's properties.
Top

  NameDescription
Public propertyActionUrlGets or sets the target URL for the postback of a Web Forms page.
Public propertyArgumentGets or sets an optional argument that is transferred in the postback event.
Public propertyAutoPostBackGets or sets a value that indicates whether the form will automatically post back to the server in response to a user action.
Public propertyClientSubmitGets or sets a value indicating whether the postback event should occur from client-side script.
Public propertyPerformValidationGets or sets a value indicating whether client-side validation is required before the postback event occurs.
Public propertyRequiresJavaScriptProtocolGets or sets a value indicating whether the javascript: prefix is generated for the client-side script.
Public propertyTargetControlGets the control target that receives the postback event.
Public propertyTrackFocusGets or sets a value indicating whether the postback event should return the page to the current scroll position and return focus to the current control.
Public propertyValidationGroupGets or sets the group of controls for which the PostBackOptions object causes validation when it posts back to the server.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The PostBackOptions class allows controls to emit client-side script that initiates a postback event. The PostBackOptions class also provides a reference to the control that initiated the postback event through the TargetControl property. The postback event is created based on the options specified in the PostBackOptions object passed in to the ClientScriptManager.GetPostBackEventReference method.

Normally, a postback to the server is initiated by elements such as a Submit button or an Image button. However, by emitting client-side JavaScript, different controls can initiate a postback event.

The following code example uses the OnClick event of a Button control to generate client-side script for a HyperLink control that will allow the HyperLink control to cause a postback event. Because the ActionUrl property of the myPostBackOptions object is set to "Page2.aspx", the postback will post the Web Forms page to another page named "Page2.aspx", which is not provided here. To use this example, you must add another Web page named "Page2.aspx" to your project or directory.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class postbackoptionscs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            Label1.Text = "A postback event has occurred.";
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        // Create a new PostBackOptions object and set its properties.
        PostBackOptions myPostBackOptions = new PostBackOptions(this);
        myPostBackOptions.ActionUrl = "Page2.aspx";
        myPostBackOptions.AutoPostBack = false;
        myPostBackOptions.RequiresJavaScriptProtocol = true;
        myPostBackOptions.PerformValidation = true;

        // Add the client-side script to the HyperLink1 control.
        HyperLink1.NavigateUrl = Page.ClientScript.GetPostBackEventReference(myPostBackOptions);

        Label1.Text = "Click this hyperlink to initiate a postback event.";
    }
}

The following code example is a Web page that can be used to run the preceding code-behind file.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="postbackoptions.aspx.cs" Inherits="postbackoptionscs" %>

<!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 id="Head1" runat="server">
    <title>PostBackOptions Example</title>
  </head>
  <body>
    <form id="form1" runat="server">
    <h3>PostBackOptions Example Page</h3>
      Click this button to create client-side script for the 
      Postback hyperlink that causes a postback event to occur.
      <br />
      <asp:Button id="Button1" 
        runat="server" 
        text="Create Script" 
        onclick="Button1_Click" />
      <br /><br />
      <asp:Label id="Label1" 
        runat="server" 
        text="">
      </asp:Label>
      <br />
      <asp:HyperLink id="HyperLink1" 
        runat="server" 
        text="Postback">
      </asp:HyperLink>
    </form>
  </body>
</html>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft