ImageButton.PostBackUrl プロパティ

定義

ImageButton コントロールがクリックされたときに現在のページからのポスト先となるページの URL を取得または設定します。

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

プロパティ値

String

ImageButton コントロールがクリックされたときに現在のページからのポスト先となる Web ページの URL。 既定値は空の文字列 ("") です。この場合、ページはそのページ自体にポストバックされます。

実装

属性

次のコード例は、プロパティを使用 PostBackUrl してクロスページ投稿を実行する方法を示しています。 ユーザーがコントロールを ImageButton クリックすると、テキスト ボックスに入力された値が、プロパティで指定されたターゲット ページに PostBackUrl ポストされます。 このサンプルを実行するには、このコード例と同じディレクトリにターゲット ページ用のファイルも作成する必要があります。 ターゲット ページのコードは、次の例で提供されます。

<%@ 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>

次のコード例では、プロパティを使用して、プロパティを使用して別の Page.PreviousPage ページから投稿された値にアクセスする方法を PostBackUrl 示します。 このページは、前のページから投稿された文字列を取得し、ユーザーに表示します。 このコード例を直接実行しようとすると、フィールドnullText値が . 代わりに、このコードを使用してターゲット ページを作成し、前の例のコードと同じディレクトリにファイルを配置します。 ファイルの名前は、前の例でプロパティに指定した PostBackUrl 値に対応している必要があります。 前の例のコードを実行すると、クロスページ投稿が発生すると、このページが自動的に実行されます。

注意

次のコード サンプルでは、単一ファイルのコード モデルを使用します。分離コード ファイルに直接コピーすると、正しく動作しない場合があります。 このコード サンプルは、拡張子が .aspx の空のテキスト ファイルにコピーする必要があります。 Web Forms コード モデルの詳細については、「ASP.NET Web Forms ページ コード モデル」を参照してください。

<%@ 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>

注釈

この PostBackUrl プロパティを使用すると、コントロールを使用してクロスページ投稿を ImageButton 実行できます。 コントロールが PostBackUrl クリックされたときに投稿する Web ページの URL にプロパティを ImageButton 設定します。 たとえば、指定すると Page2.aspx 、コントロールを含むページが ImageButton ポストされます Page2.aspx。 プロパティの値 PostBackUrl を指定しない場合、ページはそれ自体にポストバックします。

重要

サーバー側の検証を使用してコントロールを使用してページ間ポストバックを実行する場合は、ポストバックを処理する前にページの IsValid プロパティがあることを true 確認する必要があります。 クロスページ ポストバックの場合、確認するページは PreviousPage です。 次のVisual Basicコードは、この方法を示しています。

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  

クロスページ転記手法の詳細については、「ASP.NET Web Formsでのクロスページ転記」を参照してください。

このプロパティは、テーマまたはスタイル シート テーマによって設定することはできません。 詳細については、テーマとスキンの ASP.NET を参照してくださいThemeableAttribute

適用対象

こちらもご覧ください