QueryStringParameter クラス

定義

パラメーター オブジェクトに HTTP 要求のクエリ文字列フィールドの値をバインドします。

public ref class QueryStringParameter : System::Web::UI::WebControls::Parameter
public class QueryStringParameter : System.Web.UI.WebControls.Parameter
type QueryStringParameter = class
    inherit Parameter
Public Class QueryStringParameter
Inherits Parameter
継承
QueryStringParameter

次の例では、コントロールにデータを QueryStringParameter 表示するときにフィルターとして使用するオブジェクトを作成する方法を GridView 示します。 オブジェクトをQueryStringParameterコントロールFilterParametersのコレクションにAccessDataSource追加します。 パラメーター オブジェクトは、 という名前 country のクエリ文字列フィールドの値をその FilterExpression 文字列にバインドします。 パラメーターにプロパティが指定されていないため DefaultValue 、 という名前 country のフィールドがクエリ文字列と共に渡されない場合、 AccessDataSource コントロールは例外を NullReferenceException スローします。 という名前 country のフィールドが渡されても値がない場合、コントロールには GridView データは表示されません。

<%@ 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 runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" method="post" runat="server">

      <!-- Use a Query String with country=USA -->
      <asp:gridview
        id ="GridView1"
        runat="server"
        datasourceid="MyAccessDataSource" />

<!-- Security Note: The AccessDataSource uses a QueryStringParameter,
     Security Note: which does not perform validation of input from the client. -->

      <asp:accessdatasource
        id="MyAccessDataSource"
        runat="server"
        datafile="Northwind.mdb"
        selectcommand="SELECT EmployeeID, LastName, Address, PostalCode, Country FROM Employees"
        filterexpression="Country = '{0}'">
        <filterparameters>
          <asp:querystringparameter name="country" type="String" querystringfield="country" />
        </filterparameters>
      </asp:accessdatasource>
    </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 runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" method="post" runat="server">

      <!-- Use a Query String with country=USA -->
      <asp:gridview
        id ="GridView1"
        runat="server"
        datasourceid="MyAccessDataSource" />

<!-- Security Note: The AccessDataSource uses a QueryStringParameter,
     Security Note: which does not perform validation of input from the client. -->

      <asp:accessdatasource
        id="MyAccessDataSource"
        runat="server"
        datafile="Northwind.mdb"
        selectcommand="SELECT EmployeeID, LastName, Address, PostalCode, Country FROM Employees"
        filterexpression="Country = '{0}'">
        <filterparameters>
          <asp:querystringparameter name="country" type="String" querystringfield="country" />
        </filterparameters>
      </asp:accessdatasource>

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

次の例では、パラメーター化された SQL クエリを QueryStringParameter 使用して Access データベースのデータを表示するオブジェクトを作成する方法を示します。 オブジェクトは AccessDataSource 、コントロールに表示されるレコードを GridView 取得します。 GridViewコントロールも編集可能であり、ユーザーは Northwind Traders Orders テーブルの注文の状態を更新できます。

<%@Page  Language="C#" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
private void UpdateRecords(Object source, EventArgs e)
{
  // This method is an example of batch updating using a
  // data source control. The method iterates through the rows
  // of the GridView, extracts each CheckBox from the row and, if
  // the CheckBox is checked, updates data by calling the Update
  // method of the data source control, adding required parameters
  // to the UpdateParameters collection.
  CheckBox cb;
  foreach(GridViewRow row in this.GridView1.Rows) {
    cb = (CheckBox) row.Cells[0].Controls[1];
    if(cb.Checked) {
      string oid = (string) row.Cells[1].Text;
      MyAccessDataSource.UpdateParameters.Add(new Parameter("date",TypeCode.DateTime,DateTime.Now.ToString()));
      MyAccessDataSource.UpdateParameters.Add(new Parameter("orderid",TypeCode.String,oid));
      MyAccessDataSource.Update();
      MyAccessDataSource.UpdateParameters.Clear();
    }
  }
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

<!-- Security Note: The SqlDataSource uses a QueryStringParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the QueryStringParameter, handle the Selecting event. -->

      <asp:SqlDataSource
        id="MyAccessDataSource"
        runat="server"
        ProviderName="<%$ ConnectionStrings:MyPasswordProtectedAccess.providerName%>"
        ConnectionString="<%$ ConnectionStrings:MyPasswordProtectedAccess%>"
        SelectCommand="SELECT OrderID, OrderDate, RequiredDate, ShippedDate FROM Orders WHERE EmployeeID=?"
        UpdateCommand="UPDATE Orders SET ShippedDate=? WHERE OrderID = ?">
        <SelectParameters>
          <asp:QueryStringParameter Name="empId" QueryStringField="empId" />
        </SelectParameters>
      </asp:SqlDataSource>

      <asp:GridView
        id ="GridView1"
        runat="server"
        DataSourceID="MyAccessDataSource"
        AllowPaging="True"
        PageSize="10"
        AutoGenerateColumns="False">
          <columns>
            <asp:TemplateField HeaderText="">
              <ItemTemplate>
                <asp:CheckBox runat="server" />
              </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Order" DataField="OrderID" />
            <asp:BoundField HeaderText="Order Date" DataField="OrderDate" />
            <asp:BoundField HeaderText="Required Date" DataField="RequiredDate" />
            <asp:BoundField HeaderText="Shipped Date" DataField="ShippedDate" />
          </columns>
      </asp:GridView>

      <asp:Button
        id="Button1"
        runat="server"
        Text="Update the Selected Records As Shipped"
        OnClick="UpdateRecords" />

      <asp:Label id="Label1" runat="server" />

    </form>
  </body>
</html>
<%@Page  Language="VB" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Private Sub UpdateRecords(source As Object, e As EventArgs)

  ' This method is an example of batch updating using a
  ' data source control. The method iterates through the rows
  ' of the GridView, extracts each CheckBox from the row and, if
  ' the CheckBox is checked, updates data by calling the Update
  ' method of the data source control, adding required parameters
  ' to the UpdateParameters collection.

  Dim cb As CheckBox
  Dim row As GridViewRow

  For Each row In GridView1.Rows

    cb = CType(row.Cells(0).Controls(1), CheckBox)
    If cb.Checked Then

      Dim oid As String
      oid = CType(row.Cells(1).Text, String)

      Dim param1 As New Parameter("date", TypeCode.DateTime, DateTime.Now.ToString())
      MyAccessDataSource.UpdateParameters.Add(param1)

      Dim param2 As New Parameter("orderid", TypeCode.String, oid)
      MyAccessDataSource.UpdateParameters.Add(param2)

      MyAccessDataSource.Update()
      MyAccessDataSource.UpdateParameters.Clear()
    End If
  Next
End Sub ' UpdateRecords
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

<!-- Security Note: The SqlDataSource uses a QueryStringParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the QueryStringParameter, handle the Selecting event. -->

      <asp:SqlDataSource
        id="MyAccessDataSource"
        runat="server"
        ProviderName="<%$ ConnectionStrings:MyPasswordProtectedAccess.providerName%>"
        ConnectionString="<%$ ConnectionStrings:MyPasswordProtectedAccess%>"
        SelectCommand="SELECT OrderID, OrderDate, RequiredDate, ShippedDate FROM Orders WHERE EmployeeID=?"
        UpdateCommand="UPDATE Orders SET ShippedDate=? WHERE OrderID = ?">
        <SelectParameters>
          <asp:QueryStringParameter Name="empId" QueryStringField="empId" />
        </SelectParameters>
      </asp:SqlDataSource>

      <asp:GridView
        id ="GridView1"
        runat="server"
        DataSourceID="MyAccessDataSource"
        AllowPaging="True"
        PageSize="10"
        AutoGenerateColumns="False">
          <columns>
            <asp:TemplateField HeaderText="">
              <ItemTemplate>
                <asp:CheckBox runat="server" />
              </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Order" DataField="OrderID" />
            <asp:BoundField HeaderText="Order Date" DataField="OrderDate" />
            <asp:BoundField HeaderText="Required Date" DataField="RequiredDate" />
            <asp:BoundField HeaderText="Shipped Date" DataField="ShippedDate" />
          </columns>
      </asp:GridView>

      <asp:Button
        id="Button1"
        runat="server"
        Text="Update the Selected Records As Shipped"
        OnClick="UpdateRecords" />

      <asp:Label id="Label1" runat="server" />

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

注釈

クラスを QueryStringParameter 使用すると、HTTP 要求クエリ文字列の一部として渡されるフィールドの値を、パラメーター化されたクエリまたはコマンドで使用されるパラメーターにバインドできます。 フィールドはコレクションから QueryString 取得されます。

オブジェクトが参照されているが、対応するクエリ文字列の名前と値のペアが渡されない場合 QueryStringParameter 、パラメーターにデータをバインドするコントロールは例外をスローする可能性があります。 同様に、クエリ文字列フィールド名が対応する値なしで渡された場合、データが表示されない場合があります。 このような状況を回避するには、必要に応じて プロパティを設定します DefaultValue

クラスは QueryStringParameter 、 プロパティを QueryStringField 提供します。これは、バインド先のクエリ文字列値の名前を識別します。 また、 クラスから Parameter 継承されるプロパティも提供されます。

重要

クラスは QueryStringParameter 、渡された値を検証しません。生の値を提供します。 ただし、データ ソース コントロール内のオブジェクトの QueryStringParameter 値を検証できます。 これを行うには、データ ソース 管理の SelectingUpdatingInserting、または Deleting イベントを処理し、イベント ハンドラーでパラメーター値をチェックします。 パラメーターの値が検証テストに合格しない場合は、関連付けられているCancelEventArgsクラスの プロパティを にtrue設定Cancelすることで、データ操作を取り消すことができます。

コンストラクター

QueryStringParameter()

QueryStringParameter クラスの名前のない新しいインスタンスを初期化します。

QueryStringParameter(QueryStringParameter)

original パラメーターで指定されたインスタンスの値を使用して、QueryStringParameter クラスの新しいインスタンスを初期化します。

QueryStringParameter(String, DbType, String)

指定されたクエリ文字列フィールドとパラメーターのデータ型を使用し、QueryStringParameter クラスの名前付きの新しいインスタンスを初期化します。

QueryStringParameter(String, String)

バインド先のクエリ文字列フィールドを識別する文字列を指定して、QueryStringParameter クラスの名前付きの新しいインスタンスを初期化します。

QueryStringParameter(String, TypeCode, String)

バインド先のクエリ文字列フィールドを識別する文字列を指定して、QueryStringParameter クラスの名前付きの厳密に型指定された新しいインスタンスを初期化します。

プロパティ

ConvertEmptyStringToNull

Parameter オブジェクトのバインド先の値が Empty の場合に、その値を null に変換する必要があるかどうかを示す値を取得または設定します。

(継承元 Parameter)
DbType

パラメーターのデータベース型を取得または設定します。

(継承元 Parameter)
DefaultValue

パラメーターの既定値を指定します。Evaluate(HttpContext, Control) メソッドの呼び出し時に、パラメーターはこの値にバインドされ、初期化前の状態に戻されます。

(継承元 Parameter)
Direction

Parameter オブジェクトを使用して値をコントロールにバインドするかどうか、またはそのコントロールを使用して値を変更できるかどうかを示します。

(継承元 Parameter)
IsTrackingViewState

Parameter オブジェクトがビューステートへの変更を保存しているかどうかを示す値を取得します。

(継承元 Parameter)
Name

パラメーターの名前を取得または設定します。

(継承元 Parameter)
QueryStringField

パラメーターのバインド先のクエリ文字列フィールドの名前を取得または設定します。

Size

パラメーターのサイズを取得または設定します。

(継承元 Parameter)
Type

パラメーターの型を取得または設定します。

(継承元 Parameter)
ValidateInput

クエリ文字列パラメーターの値が検証されるかどうかを取得または設定します。

ViewState

同一のページに対する複数の要求にわたって、Parameter オブジェクトのビューステートを保存し、復元できるようにする状態情報のディクショナリを取得します。

(継承元 Parameter)

メソッド

Clone()

現在の QueryStringParameter インスタンスの複製を返します。

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Evaluate(HttpContext, Control)

QueryStringParameter オブジェクトの値を更新して返します。

GetDatabaseType()

現在の DbType インスタンスの CLR 型と等価な Parameter 値を取得します。

(継承元 Parameter)
GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
LoadViewState(Object)

データ ソース ビューの、以前保存したビューステートを復元します。

(継承元 Parameter)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
OnParameterChanged()

OnParametersChanged(EventArgs) オブジェクトを格納する ParameterCollection コレクションの Parameter メソッドを呼び出します。

(継承元 Parameter)
SaveViewState()

ページがサーバーにポスト バックされた時間以降に発生した、Parameter オブジェクトのビューステートへの変更を保存します。

(継承元 Parameter)
SetDirty()

Parameter オブジェクトの状態がビューステートで記録されるように、このオブジェクトをマークします。

(継承元 Parameter)
ToString()

このインスタンスの値を、それと等価の文字列形式に変換します。

(継承元 Parameter)
TrackViewState()

Parameter オブジェクトがビューステートの変更を追跡するようにします。それにより、変更をコントロールの ViewState オブジェクトに格納して、同じページに対する複数の要求にわたって永続化できます。

(継承元 Parameter)

明示的なインターフェイスの実装

ICloneable.Clone()

現在の Parameter インスタンスの複製を返します。

(継承元 Parameter)
IStateManager.IsTrackingViewState

Parameter オブジェクトがビューステートへの変更を保存しているかどうかを示す値を取得します。

(継承元 Parameter)
IStateManager.LoadViewState(Object)

データ ソース ビューの、以前保存したビューステートを復元します。

(継承元 Parameter)
IStateManager.SaveViewState()

ページがサーバーにポスト バックされた時間以降に発生した、Parameter オブジェクトのビューステートへの変更を保存します。

(継承元 Parameter)
IStateManager.TrackViewState()

Parameter オブジェクトがビューステートの変更を追跡するようにします。それにより、変更をコントロールの ViewState オブジェクトに格納して、同じページに対する複数の要求にわたって永続化できます。

(継承元 Parameter)

適用対象

こちらもご覧ください