Export (0) Print
Expand All

Page.GetPostBackEventReference Method

Obtains a reference to a client-side script function that causes, when invoked, the server to post back to the page.

Overload List

Obtains a reference to a client-side script function that causes, when invoked, the server to post back to the page.

[Visual Basic] Overloads Public Function GetPostBackEventReference(Control) As String
[C#] public string GetPostBackEventReference(Control);
[C++] public: String* GetPostBackEventReference(Control*);
[JScript] public function GetPostBackEventReference(Control) : String;

Obtains a reference to a client-side script function that causes, when invoked, the server to post back to the page. This method also passes a parameter to the server control that performs the post-back processing on the server.

[Visual Basic] Overloads Public Function GetPostBackEventReference(Control, String) As String
[C#] public string GetPostBackEventReference(Control, string);
[C++] public: String* GetPostBackEventReference(Control*, String*);
[JScript] public function GetPostBackEventReference(Control, String) : String;

Example

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of GetPostBackEventReference. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Public Class MyControl
   Inherits Control
   Implements IPostBackEventHandler

   ' Create an integer property that is displayed when
   ' the page that contains this control is requested
   ' and save it to the control's ViewState property.      
   Public Property Number() As Integer
      Get
         If Not (ViewState("Number") Is Nothing) Then
            Return CInt(ViewState("Number"))
         End If
         Return 50
      End Get
      
      Set
         ViewState("Number") = value
      End Set
   End Property      
   
   ' Implement the RaisePostBackEvent method from the
   ' IPostBackEventHandler interface. If inc is passed
   ' to this method, it increases the Number property by one.
   ' If dec is passed to this method, it decreases the
   ' Number property by one.
   Sub RaisePostBackEvent(eventArgument As String) Implements IPostBackEventHandler.RaisePostBackEvent

      If eventArgument = "inc" Then
         Number = Number + 1
      End If 
      If eventArgument = "dec" Then
         Number = Number - 1
      End If
   End Sub 'RaisePostBackEvent
   
   <System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _ 
   Protected Overrides Sub Render(writer As HtmlTextWriter)
      ' Converts the Number property to a string and
  ' writes it to the containing page.
      writer.Write(("The Number is " + Number.ToString() + " ("))
      
  ' Uses the GetPostBackEventReference method to pass
  ' inc to the RaisePostBackEvent method when the link
  ' this code creates is clicked.
      writer.Write(("<a href=""javascript:" + Page.GetPostBackEventReference(Me, "inc") + """>Increase Number</a>"))
      
      writer.Write(" or ")

  ' Uses the GetPostBackEventReference method to pass
  ' dec to the RaisePostBackEvent method when the link
  ' this code creates is clicked.         
      writer.Write(("<a href=""javascript:" + Page.GetPostBackEventReference(Me, "dec") + """>Decrease Number</a>"))
   End Sub 'Render
End Class 'MyControl

[C#] 
public class MyControl : Control, IPostBackEventHandler
{
   
   // Create an integer property that is displayed when
   // the page that contains this control is requested
   // and save it to the control's ViewState property.
   public int Number
   {
     get
     {
       if ( ViewState["Number"] !=null )
          return (int) ViewState["Number"];
       return 50;
     }

     set
     {
       ViewState["Number"] = value;
     }        
   }

   // Implement the RaisePostBackEvent method from the
   // IPostBackEventHandler interface. If 'inc' is passed
   // to this method, it increases the Number property by one.
   // If 'dec' is passed to this method, it decreases the
   // Number property by one.
   public void RaisePostBackEvent(string eventArgument)
   {
     if ( eventArgument == "inc" )
     Number = Number + 1;

     if ( eventArgument == "dec" )
     Number = Number - 1;
   }

   
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
   protected override void Render(HtmlTextWriter writer)
   {
     // Converts the Number property to a string and
 // writes it to the containing page.
     writer.Write("The Number is " + Number.ToString() + " (" );

 // Uses the GetPostBackEventReference method to pass
 // 'inc' to the RaisePostBackEvent method when the link
 // this code creates is clicked.
     writer.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this,"inc") + "\">Increase Number</a>"); 

     writer.Write(" or ");

 // Uses the GetPostBackEventReference method to pass
 // 'dec' to the RaisePostBackEvent method when the link
 // this code creates is clicked.
     writer.Write("<a href=\"javascript:" + Page.GetPostBackEventReference(this,"dec") + "\">Decrease Number</a>");
   }
}

[C++] 
public __gc class MyControl : public Control, public IPostBackEventHandler
{
   // Create an integer property that is displayed when
   // the page that contains this control is requested
   // and save it to the control's ViewState property.
public:
   __property int get_Number() 
   {
      if (ViewState->get_Item(S"Number") !=0)
         return * dynamic_cast<__box int*>(ViewState->get_Item(S"Number"));
      return 50;
   }

   __property void set_Number(int value) 
   {
      ViewState->Item[S"Number"] = __box(value);
   }

   // Implement the RaisePostBackEvent method from the
   // IPostBackEventHandler* interface. If 'inc' is passed
   // to this method, it increases the Number property by one.
   // If 'dec' is passed to this method, it decreases the
   // Number property by one.
   void RaisePostBackEvent(String* eventArgument)
   {
      if (String::Compare(eventArgument, S"inc") == 0)
         Number = Number + 1;

      if (String::Compare(eventArgument, S"dec") == 0)
         Number = Number - 1;
   }

protected:
   [System::Security::Permissions::PermissionSet(System::Security::Permissions::SecurityAction::Demand, Name="FullTrust")]
   void Render(HtmlTextWriter* writer)
   {
      // Converts the Number property to a String* and
      // writes it to the containing page.
      writer->Write(S"The Number is {0} (", __box(Number));

      // Uses the GetPostBackEventReference method to pass
      // 'inc' to the RaisePostBackEvent method when the link
      // this code creates is clicked.
      writer->Write(S"<a href=\"javascript: {0} \">Increase Number</a>", 
         Page->GetPostBackEventReference(this,S"inc"));

      writer->Write(S" or ");

      // Uses the GetPostBackEventReference method to pass
      // 'dec' to the RaisePostBackEvent method when the link
      // this code creates is clicked.
      writer->Write(S"<a href=\"javascript:S {0} \">Decrease Number</a>", 
         Page->GetPostBackEventReference(this, S"decS"));
   }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

Page Class | Page Members | System.Web.UI Namespace

Show:
© 2014 Microsoft