Parameter クラス

定義

アプリケーション変数、ユーザー ID とユーザー選択、および他のデータにバインドするためにデータ ソース コントロールで使用する機構を提供します。 ASP.NET のすべてのパラメーター型の基底クラスとして機能します。

public ref class Parameter : ICloneable, System::Web::UI::IStateManager
public class Parameter : ICloneable, System.Web.UI.IStateManager
type Parameter = class
    interface ICloneable
    interface IStateManager
Public Class Parameter
Implements ICloneable, IStateManager
継承
Parameter
派生
実装

次の例は、SQL クエリの Where 句でコントロールのDropDownList選択した値を使用する方法を示しています。 この例では、 ControlParameter クラスから派生した クラスを ControlParameter 使用します。

要素は SelectCommand 、 の値 DropDownList1 が格納される "@Title" という名前のパラメーターを使用してクエリを定義します。 要素はControlParameter、"@Title" プレースホルダーをコントロールの プロパティDropDownList1SelectedValue値に置き換えることを指定します。 ControlParameter要素は、コントロールのSelectParametersコレクションにSqlDataSource追加されます。

<!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" runat="server">

      <p><asp:dropdownlist
          id="DropDownList1"
          runat="server"
          autopostback="True">
          <asp:listitem selected="True">Sales Representative</asp:listitem>
          <asp:listitem>Sales Manager</asp:listitem>
          <asp:listitem>Vice President, Sales</asp:listitem>
      </asp:dropdownlist></p>

      <asp:sqldatasource
          id="SqlDataSource1"
          runat="server"
          connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
          selectcommand="SELECT LastName FROM Employees WHERE Title = @Title">
          <selectparameters>
              <asp:controlparameter name="Title" controlid="DropDownList1" propertyname="SelectedValue"/>
          </selectparameters>
      </asp:sqldatasource>

      <p><asp:listbox
          id="ListBox1"
          runat="server"
          datasourceid="SqlDataSource1"
          datatextfield="LastName">
      </asp:listbox></p>

    </form>
  </body>
</html>
<!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" runat="server">

      <p><asp:dropdownlist
          id="DropDownList1"
          runat="server"
          autopostback="True">
          <asp:listitem selected="True">Sales Representative</asp:listitem>
          <asp:listitem>Sales Manager</asp:listitem>
          <asp:listitem>Vice President, Sales</asp:listitem>
      </asp:dropdownlist></p>

      <asp:sqldatasource
          id="SqlDataSource1"
          runat="server"
          connectionstring="<%$ ConnectionStrings:MyNorthwind%>"
          selectcommand="SELECT LastName FROM Employees WHERE Title = @Title">
          <selectparameters>
              <asp:controlparameter name="Title" controlid="DropDownList1" propertyname="SelectedValue"/>
          </selectparameters>
      </asp:sqldatasource>

      <p><asp:listbox
          id="ListBox1"
          runat="server"
          datasourceid="SqlDataSource1"
          datatextfield="LastName">
      </asp:listbox></p>

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

次の例は前の例に似ていますが、マークアップの代わりにコードを使用します。 ページが初めて読み込まれるとDefaultValue、コントロールにはDropDownList選択された値がなく、オブジェクトの Parameter プロパティが使用されます。

<%@ Page Language="C#" CodeFile="param1acs.aspx.cs" Inherits="param1acs_aspx" %>
<!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" runat="server">
    <div>
        <asp:DropDownList
          runat="server"
          AutoPostBack="True"
          id="DropDownList1">
            <asp:ListItem Value="USA">USA</asp:ListItem>
            <asp:ListItem Value="UK">UK</asp:ListItem>
         </asp:DropDownList>

        <asp:DataGrid
          runat="server"
          id="DataGrid1" />    
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="param1avb.aspx.vb" Inherits="param1avb_aspx" %>
<!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" runat="server">
    <div>
        <asp:DropDownList
          runat="server"
          AutoPostBack="True"
          id="DropDownList1">
            <asp:ListItem Value="USA">USA</asp:ListItem>
            <asp:ListItem Value="UK">UK</asp:ListItem>
         </asp:DropDownList>

        <asp:DataGrid
          runat="server"
          id="DataGrid1" />    
    </div>
    </form>
</body>
</html>

次のコードは、前の例のページの分離コード クラスを示しています。

public partial class param1acs_aspx : System.Web.UI.Page 
{
    private void Page_Load(object sender, System.EventArgs e)
    {
        SqlDataSource sqlSource = new SqlDataSource(
          ConfigurationManager.ConnectionStrings["MyNorthwind"].ConnectionString,
          "SELECT FirstName, LastName FROM Employees WHERE Country = @country;");

        ControlParameter country = new ControlParameter();
        country.Name = "country";
        country.Type = TypeCode.String;
        country.ControlID = "DropDownList1";
        country.PropertyName = "SelectedValue";

        // If the DefaultValue is not set, the DataGrid does not
        // display anything on the first page load. This is because
        // on the first page load, the DropDownList has no
        // selected item, and the ControlParameter evaluates to
        // String.Empty.
        country.DefaultValue = "USA";

        sqlSource.SelectParameters.Add(country);

        // Add the SqlDataSource to the page controls collection.
        Page.Controls.Add(sqlSource);

        DataGrid1.DataSource = sqlSource;
        DataGrid1.DataBind();
    }
}
Partial Class param1avb_aspx
   Inherits System.Web.UI.Page
    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim sqlSource As SqlDataSource

        sqlSource = New SqlDataSource(ConfigurationManager.ConnectionStrings("MyNorthwind").ConnectionString, "SELECT FirstName, LastName FROM Employees WHERE Country = @country;")
        Dim country As New ControlParameter()
        country.Name = "country"
        country.Type = TypeCode.String
        country.ControlID = "DropDownList1"
        country.PropertyName = "SelectedValue"
        ' If the DefaultValue is not set, the DataGrid does not
        ' display anything on the first page load. This is because
        ' on the first page load, the DropDownList has no
        ' selected item, and the ControlParameter evaluates to
        ' String.Empty.
        country.DefaultValue = "USA"
        sqlSource.SelectParameters.Add(country)

        ' Add the SqlDataSource to the page controls collection.
        Page.Controls.Add(sqlSource)


        DataGrid1.DataSource = sqlSource
        DataGrid1.DataBind()

    End Sub
End Class

次のコード例では、 クラスを Parameter 拡張して、データ バインディング シナリオでデータ ソース コントロールやその他のコントロールで使用できる新しいパラメーター型を作成する方法を示します。 データ ソース コントロールでは、 パラメーターをStaticParameter使用して、Web Forms ページで宣言されている任意のオブジェクト (通常は文字列) の値にバインドできます。

namespace Samples.AspNet {

  using System;
  using System.ComponentModel;
  using System.Security.Permissions;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;

  [AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
  public class StaticParameter : Parameter {

    public StaticParameter() {
    }
    // The StaticParameter(string, object) constructor
    // initializes the DataValue property and calls the
    // Parameter(string) constructor to initialize the Name property.
    public StaticParameter(string name, object value) : base(name) {
      DataValue = value;
    }
    // The StaticParameter(string, TypeCode, object) constructor
    // initializes the DataValue property and calls the
    // Parameter(string, TypeCode) constructor to initialize the Name and
    // Type properties.
    public StaticParameter(string name, TypeCode type, object value) : base(name, type) {
      DataValue = value;
    }
    // The StaticParameter copy constructor is provided to ensure that
    // the state contained in the DataValue property is copied to new
    // instances of the class.
    protected StaticParameter(StaticParameter original) : base(original) {
      DataValue = original.DataValue;
    }

    // The Clone method is overridden to call the
    // StaticParameter copy constructor, so that the data in
    // the DataValue property is correctly transferred to the
    // new instance of the StaticParameter.
    protected override Parameter Clone() {
      return new StaticParameter(this);
    }
    // The DataValue can be any arbitrary object and is stored in ViewState.
    public object DataValue {
      get {
        return ViewState["Value"];
      }
      set {
        ViewState["Value"] = value;
      }
    }
    // The Value property is a type safe convenience property
    // used when the StaticParameter represents string data.
    // It gets the string value of the DataValue property, and
    // sets the DataValue property directly.
    public string Value {
      get {
        object o = DataValue;
        if (o == null || !(o is string))
          return String.Empty;
        return (string)o;
      }
      set {
        DataValue = value;
        OnParameterChanged();
      }
    }

    // The Evaluate method is overridden to return the
    // DataValue property instead of the DefaultValue.
    protected override object Evaluate(HttpContext context, Control control) {

      if (context.Request == null)
          return null;

      return DataValue;
    }
  }
}
Imports System.ComponentModel
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace Samples.AspNet

<AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class StaticParameter
   Inherits Parameter


   Public Sub New()
   End Sub

  ' The StaticParameter(string, object) constructor
  ' initializes the DataValue property and calls the
  ' Parameter(string) constructor to initialize the Name property.
   Public Sub New(name As String, value As Object)
      MyBase.New(name)
      DataValue = value
   End Sub

   ' The StaticParameter(string, TypeCode, object) constructor
   ' initializes the DataValue property and calls the
   ' Parameter(string, TypeCode) constructor to initialize the Name and
   ' Type properties.
   Public Sub New(name As String, type As TypeCode, value As Object)
      MyBase.New(name, type)
      DataValue = value
   End Sub
   ' The StaticParameter copy constructor is provided to ensure that
   ' the state contained in the DataValue property is copied to new
   ' instances of the class.
   Protected Sub New(original As StaticParameter)
      MyBase.New(original)
      DataValue = original.DataValue
   End Sub

   ' The Clone method is overridden to call the
   ' StaticParameter copy constructor, so that the data in
   ' the DataValue property is correctly transferred to the
   ' new instance of the StaticParameter.
   Protected Overrides Function Clone() As Parameter
      Return New StaticParameter(Me)
   End Function

   ' The DataValue can be any arbitrary object and is stored in ViewState.
   Public Property DataValue() As Object
      Get
         Return ViewState("Value")
      End Get
      Set
         ViewState("Value") = value
      End Set
   End Property
   ' The Value property is a type safe convenience property
   ' used when the StaticParameter represents string data.
   ' It gets the string value of the DataValue property, and
   ' sets the DataValue property directly.
   Public Property Value() As String
      Get
         Dim o As Object = DataValue
         If o Is Nothing OrElse Not TypeOf o Is String Then
            Return String.Empty
         End If
         Return CStr(o)
      End Get
      Set
         DataValue = value
         OnParameterChanged()
      End Set
   End Property
   ' The Evaluate method is overridden to return the
   ' DataValue property instead of the DefaultValue.
   Protected Overrides Function Evaluate(context As HttpContext, control As Control) As Object
      If context Is Nothing Then
          Return Nothing
      Else
          Return DataValue
      End If
   End Function
End Class

End Namespace ' Samples.AspNet

注釈

クラスは Parameter 、パラメーター化された SQL クエリ、フィルター式、またはビジネス オブジェクト メソッドの呼び出しで、ASP.NET データ ソース コントロールがデータの選択、フィルター処理、または変更に使用するパラメーターを表します。 Parameter オブジェクトは、ParameterCollection オブジェクトに含まれています。 Parameter オブジェクトは実行時に評価され、それらが表す変数の値を、データ ソース コントロールがデータとやり取りするために使用されるメソッドにバインドされます。

から派生 Parameter したクラスをデータ ソースおよびデータ バインド コントロールと共に使用して、Web ベースのデータ アプリケーションを構築します。 これらのパラメーター クラスは、データ ソース コントロールによって使用され、Web アプリケーションで見つかった特定の種類の値を SQL クエリ文字列、ビジネス オブジェクト メソッド パラメーターなどのプレースホルダーにバインドします。 次の表に、ASP.NET に含まれるパラメーターの型を示します。

ControlParameter Web サーバー コントロールのパブリック プロパティをバインドします。
FormParameter フォーム フィールドをバインドします。
SessionParameter セッション状態フィールドをバインドします。
RouteParameter ルート URL パラメーターをバインドします。
CookieParameter Cookie フィールドをバインドします。
QueryStringParameter クエリ文字列パラメーターをバインドします。
ProfileParameter プロファイル フィールドをバインドします。

独自のカスタム パラメーター型を実装する場合は、基本 Parameter クラスを拡張します。

Parameterオブジェクトは非常に単純です。これらは、 プロパティと プロパティをType持ちName、宣言的に表すことができます。また、複数の HTTP 要求にわたって状態を追跡できます。 パラメーターが値に DefaultValue バインドされているが、実行時に 値が に評価される場合は、すべてのパラメーターで プロパティがサポートされます null

データ ソース コントロールでオブジェクトの Parameter コレクションを使用する場合、コレクション内の順序が重要になる場合があります。 パラメーターの使用方法の詳細については、「SqlDataSource コントロールでのパラメーターの使用」および「ObjectDataSource コントロールでのパラメーターの使用」を参照してください。

コンストラクター

Parameter()

Parameter クラスの新しい既定のインスタンスを初期化します。

Parameter(Parameter)

指定した元のインスタンスの値を使用して、Parameter クラスの新しいインスタンスを初期化します。

Parameter(String)

名前を指定して、Parameter クラスの新しいインスタンスを初期化します。

Parameter(String, DbType)

指定した名前とデータベース型を使用して、Parameter クラスの新しいインスタンスを初期化します。

Parameter(String, DbType, String)

指定した名前、指定したデータベース型、およびその Parameter プロパティに指定した値を使用して、DefaultValue クラスの新しいインスタンスを初期化します。

Parameter(String, TypeCode)

名前と型を指定して、Parameter クラスの新しいインスタンスを初期化します。

Parameter(String, TypeCode, String)

Parameter プロパティに指定した名前、型、文字列を使用して、DefaultValue クラスの新しいインスタンスを初期化します。

プロパティ

ConvertEmptyStringToNull

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

DbType

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

DefaultValue

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

Direction

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

IsTrackingViewState

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

Name

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

Size

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

Type

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

ViewState

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

メソッド

Clone()

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

ConvertDbTypeToTypeCode(DbType)

DbType 値を等価な TypeCode 値に変換します。

ConvertTypeCodeToDbType(TypeCode)

TypeCode 値を等価な DbType 値に変換します。

Equals(Object)

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

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

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

GetDatabaseType()

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

GetHashCode()

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

(継承元 Object)
GetType()

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

(継承元 Object)
LoadViewState(Object)

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

MemberwiseClone()

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

(継承元 Object)
OnParameterChanged()

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

SaveViewState()

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

SetDirty()

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

ToString()

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

TrackViewState()

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

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

ICloneable.Clone()

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

IStateManager.IsTrackingViewState

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

IStateManager.LoadViewState(Object)

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

IStateManager.SaveViewState()

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

IStateManager.TrackViewState()

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

適用対象

こちらもご覧ください