Bagikan melalui


ImageButton.PostBackUrl Properti

Definisi

Mendapatkan atau mengatur URL halaman yang akan diposting dari halaman saat ini saat ImageButton kontrol diklik.

public:
 virtual property System::String ^ PostBackUrl { System::String ^ get(); void set(System::String ^ value); };
[System.Web.UI.Themeable(false)]
public virtual string PostBackUrl { get; set; }
[<System.Web.UI.Themeable(false)>]
member this.PostBackUrl : string with get, set
Public Overridable Property PostBackUrl As String

Nilai Properti

URL halaman Web yang akan diposting dari halaman saat ini ketika ImageButton kontrol diklik. Nilai default adalah string kosong (""), yang menyebabkan halaman diposting kembali ke dirinya sendiri.

Penerapan

Atribut

Contoh

Contoh kode berikut menunjukkan cara menggunakan PostBackUrl properti untuk melakukan posting lintas halaman. Saat pengguna mengklik ImageButton kontrol, halaman memposting nilai yang dimasukkan dalam kotak teks ke halaman target yang ditentukan oleh PostBackUrl properti . Untuk menjalankan sampel ini, Anda juga harus membuat file untuk halaman target di direktori yang sama dengan contoh kode ini. Kode untuk halaman target disediakan dalam contoh berikutnya.

<%@ page language="C#" %>

<!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>ImageButton.PostBackUrl Example</title>
</head>
<body>    
  <form id="form1" runat="server">
    
    <h3>ImageButton.PostBackUrl Example</h3>

    Enter a value to post:
    <asp:textbox id="TextBox1" 
      runat="Server">
    </asp:textbox>

    <br /><br />

    <asp:imagebutton id="ImageButton1"
      imageUrl=""
      alternatetext="Post back to this page"
      runat="Server">
    </asp:imagebutton>

    <br /><br />

    <asp:imagebutton id="ImageButton2"
      imageUrl=""
      alternatetext="Post value to another page" 
      postbackurl="ImageButton.PostBackUrlPage2cs.aspx" 
      runat="Server">
    </asp:imagebutton>

  </form>
</body>
</html>
<%@ page language="VB"%>

<!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>ImageButton.PostBackUrl Example</title>
</head>
<body>    
  <form id="form1" runat="server">
    
    <h3>ImageButton.PostBackUrl Example</h3>

    Enter a value to post:
    <asp:textbox id="TextBox1" 
      runat="Server">
    </asp:textbox>

    <br /><br />

    <asp:imagebutton id="PostImageButton"
      imageUrl="Images\PostButton.jpg"
      alternatetext="Post back to this page"
      runat="Server">
    </asp:imagebutton>

    <br /><br />

    <asp:imagebutton id="CrossPostImageButton"
      imageUrl="Images\CrossPostButton.jpg"
      alternatetext="Post value to another page" 
      postbackurl="ImageButton.PostBackUrlPage2vb.aspx" 
      runat="Server">
    </asp:imagebutton>

  </form>
</body>
</html>

Contoh kode berikut menunjukkan cara menggunakan Page.PreviousPage properti untuk mengakses nilai yang diposting dari halaman lain menggunakan PostBackUrl properti . Halaman ini mendapatkan string yang diposting dari halaman sebelumnya dan menampilkannya kepada pengguna. Jika Anda mencoba menjalankan contoh kode ini secara langsung, Anda akan mendapatkan kesalahan karena nilai Text bidang akan menjadi null. Sebagai gantinya, gunakan kode ini untuk membuat halaman target dan menempatkan file di direktori yang sama dengan kode untuk contoh sebelumnya. Nama file harus sesuai dengan nilai yang ditentukan untuk PostBackUrl properti dalam contoh sebelumnya. Saat Anda menjalankan kode untuk contoh sebelumnya, halaman ini akan dijalankan secara otomatis ketika postingan lintas halaman terjadi.

Catatan

Sampel kode berikut menggunakan model kode file tunggal dan mungkin tidak berfungsi dengan benar jika disalin langsung ke file code-behind. Sampel kode ini harus disalin ke dalam file teks kosong yang memiliki ekstensi .aspx. Untuk informasi selengkapnya tentang model kode Formulir Web, lihat ASP.NET Model Kode Halaman Formulir Web.

<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  
  void Page_Load (object sender, System.EventArgs e)
  {
    string text;
    
    // Get the value of TextBox1 from the page that 
    // posted to this page.
    text = ((TextBox)PreviousPage.FindControl("TextBox1")).Text;
    
    // Check for an empty string.
    if (text != "")
      PostedLabel.Text = "The string posted from the previous page is "
                         + text + ".";
    else
      PostedLabel.Text = "An empty string was posted from the previous page.";
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>ImageButton.PostBackUrl Target Page Example</title>
</head>
<body>
  <form id="form1" runat="server">
    
    <h3>ImageButton.PostBackUrl Target Page Example</h3>
      
    <br />
    
    <asp:label id="PostedLabel"
       runat="Server">
    </asp:label>

    </form>
</body>
</html>
<%@ page language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
  Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    
    Dim text As String
    
    ' Get the value of TextBox1 from the page that posted
    ' to this page.
    text = CType((PreviousPage.FindControl("TextBox1")), TextBox).Text
       
    ' Check for an empty string.
    If Not (text = "") Then
      PostedLabel.Text = "The string posted from the previous page is " _
                         & text & "."
    Else
      PostedLabel.Text = "An empty string was posted from the previous page."
    End If
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="head1" runat="server">
  <title>ImageButton.PostBackUrl Target Page Example</title>
</head>
<body>
  <form id="form1" runat="server">
    
    <h3>ImageButton.PostBackUrl Target Page Example</h3>
       
    <br />
    
    <asp:label id="PostedLabel"
       runat="Server">
    </asp:label>

    </form>
</body>
</html>

Keterangan

Properti PostBackUrl memungkinkan Anda melakukan posting lintas halaman menggunakan ImageButton kontrol . Atur PostBackUrl properti ke URL halaman Web untuk diposting saat ImageButton kontrol diklik. Misalnya, menentukan Page2.aspx penyebab halaman yang berisi ImageButton kontrol untuk memposting ke Page2.aspx. Jika Anda tidak menentukan nilai untuk PostBackUrl properti , halaman memposting kembali ke dirinya sendiri.

Penting

Saat melakukan postback lintas halaman dengan kontrol dengan validasi sisi server, Anda harus memeriksa apakah properti halaman IsValid sebelum true memproses postback. Dalam kasus postback lintas halaman, halaman yang akan diperiksa adalah PreviousPage. Kode Visual Basic berikut menunjukkan bagaimana hal ini dilakukan:

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load  
        If Page.PreviousPage.IsValid Then  
            ' Handle the post back  
        Else  
            Response.Write("Invalid")  
        End If  
End Sub  

Untuk informasi selengkapnya tentang teknik posting lintas halaman, lihat Posting Lintas Halaman di ASP.NET Formulir Web.

Properti ini tidak dapat diatur oleh tema atau tema lembar gaya. Untuk informasi selengkapnya, lihat ThemeableAttribute dan ASP.NET Tema dan Kulit.

Berlaku untuk

Lihat juga