Language

Button.PostBackUrl プロパティ

定義

Button コントロールがクリックされたときに現在のページから投稿するページの 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

プロパティ値

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

実装

属性

例

次のコード例では、 PostBackUrl プロパティを使用してクロスページ投稿を実行する方法を示します。 ユーザーが Button コントロールをクリックすると、テキスト ボックスに入力された値が、 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>Button.PostBackUrl Example</title>
</head>
<body>    
  <form id="form1" runat="server">
    
    <h3>Button.PostBackUrl Example</h3>

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

    <br /><br />

    <asp:button id="Button1" 
      text="Post back to this page"
      runat="Server">
    </asp:button>

    <br /><br />

    <asp:button id="Button2"
      text="Post value to another page" 
      postbackurl="Button.PostBackUrlPage2cs.aspx" 
      runat="Server">
    </asp:button>

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

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

    <br /><br />

    <asp:button id="Button1" 
      text="Post back to this page"
      runat="Server">
    </asp:button>

    <br /><br />

    <asp:button id="Button2"
      text="Post value to another page" 
      postbackurl="Button.PostBackUrlPage2vb.aspx" 
      runat="Server">
    </asp:button>

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

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

Important

この例には、潜在的なセキュリティ上の脅威であるユーザー入力を受け入れるテキスト ボックスがあります。 既定では、ASP.NET Web ページでは、ユーザー入力にスクリプトや HTML 要素が含まれていないことが検証されます。 詳細については、「スクリプトの 悪用の概要」を参照してください。

<%@ 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>Button.PostBackUrl Target Page Example</title>
</head>
<body>
  <form id="form1" runat="server">
    
    <h3>Button.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>Button.PostBackUrl Target Page Example</title>
</head>
<body>
  <form id="form1" runat="server">
    
    <h3>Button.PostBackUrl Target Page Example</h3>
       
    <br />
    
    <asp:label id="PostedLabel"
       runat="Server">
    </asp:label>

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

注釈

PostBackUrl プロパティを使用すると、Button コントロールを使用してクロスページ投稿を実行できます。

Note

このプロパティでは、正しく指定されたパスのみが機能します。 たとえば、相対パス (Test/default.aspx)、絶対パス (https://localhost/WebApp/default.aspx)、仮想 (~\Test\default.aspx) が正しく機能します。 "/Test/default.aspx" や "\Test\default.aspx" などのパスの形式が正しくありません。 正しいパスの作成については、「 ASP.NET Web プロジェクト パス 」を参照してください。

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

Important

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

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 フォームでのクロスページ投稿」を参照してください。

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

適用対象

こちらもご覧ください