Parameter クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
データ ソース コントロールがアプリケーション変数、ユーザー 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要素は、"@Title" という名前のパラメーターを使用してクエリを定義DropDownList1。
ControlParameter要素は、"@Title" プレースホルダーをDropDownList1 コントロールのSelectedValue プロパティの値に置き換えることを指定します。
ControlParameter要素は、SqlDataSource コントロールのSelectParameters コレクションに追加されます。
<!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>
次の例は前の例に似ていますが、マークアップではなくコードを使用します。 ページが初めて読み込まれると、DropDownList コントロールには選択した値がなく、Parameter オブジェクトのDefaultValue プロパティが使用されます。
<%@ 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 フォーム ページで宣言されている任意のオブジェクト (通常は文字列) の値にバインドできます。
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 クラスは、ASP.NET データ ソース コントロールがデータの選択、フィルター処理、または変更に使用する、パラメーター化された SQL クエリ、フィルター式、またはビジネス オブジェクト メソッド呼び出しのパラメーターを表します。 Parameter オブジェクトは、 ParameterCollection オブジェクトに含まれています。 Parameter オブジェクトは実行時に評価され、それらが表す変数の値をデータ ソース コントロールがデータと対話するために使用するメソッドにバインドされます。
データ ソースコントロールとデータ バインド コントロールを使用して Parameter から派生したクラスを使用して、Web ベースのデータ アプリケーションを構築します。 これらのパラメーター クラスは、Web アプリケーションで見つかった特定の種類の値を SQL クエリ文字列、ビジネス オブジェクト メソッド パラメーターなどのプレースホルダーにバインドするために、データ ソース コントロールによって使用されます。 次の表に、ASP.NET に含まれるパラメーターの型を示します。
| パラメーターのタイプ | 説明 |
|---|---|
| ControlParameter | Web サーバー コントロールのパブリック プロパティをバインドします。 |
| FormParameter | フォーム フィールドをバインドします。 |
| SessionParameter | セッション状態フィールドをバインドします。 |
| RouteParameter | ルート URL パラメーターをバインドします。 |
| CookieParameter | Cookie フィールドをバインドします。 |
| QueryStringParameter | クエリ文字列パラメーターをバインドします。 |
| ProfileParameter | プロファイル フィールドをバインドします。 |
独自のカスタム パラメーター型を実装する場合は、基本 Parameter クラスを拡張します。
Parameter オブジェクトは非常に単純です。 Name と Type プロパティがあり、宣言によって表現でき、複数の HTTP 要求にわたって状態を追跡できます。 パラメーターが値にバインドされているが、実行時に値がnullに評価される場合は、すべてのパラメーターでDefaultValue プロパティがサポートされます。
データ ソース コントロールで Parameter オブジェクトのコレクションを使用する場合、コレクション内での順序が重要になる場合があります。 パラメーターの使用方法の詳細については、「SqlDataSource コントロールでのパラメーターの使用」および「ObjectDataSource コントロールでのパラメーターの使用」を参照してください。
コンストラクター
| 名前 | 説明 |
|---|---|
| Parameter() |
Parameter クラスの新しい既定のインスタンスを初期化します。 |
| Parameter(Parameter) |
指定した元のインスタンスの値を使用して、 Parameter クラスの新しいインスタンスを初期化します。 |
| Parameter(String, DbType, String) |
Parameter クラスの新しいインスタンスを、指定した名前、指定したデータベース型、およびそのDefaultValueプロパティの指定した値を使用して初期化します。 |
| Parameter(String, DbType) |
指定した名前とデータベース型を使用して、 Parameter クラスの新しいインスタンスを初期化します。 |
| Parameter(String, TypeCode, String) |
指定した名前、指定した型、およびDefaultValue プロパティの指定した文字列を使用して、Parameter クラスの新しいインスタンスを初期化します。 |
| Parameter(String, TypeCode) |
指定した名前と型を使用して、 Parameter クラスの新しいインスタンスを初期化します。 |
| Parameter(String) |
指定した名前を使用して、 Parameter クラスの新しいインスタンスを初期化します。 |
プロパティ
| 名前 | 説明 |
|---|---|
| ConvertEmptyStringToNull |
Parameter オブジェクトがバインドされている値がEmptyされている場合に |
| DbType |
パラメーターのデータベース型を取得または設定します。 |
| DefaultValue |
パラメーターの既定値を指定します。パラメーターがバインドされている値が、 Evaluate(HttpContext, Control) メソッドが呼び出されたときに初期化されないようにする必要があります。 |
| Direction |
Parameter オブジェクトを使用して値をコントロールにバインドするか、コントロールを使用して値を変更できるかを示します。 |
| IsTrackingViewState |
Parameter オブジェクトがビュー ステートへの変更を保存しているかどうかを示す値を取得します。 |
| Name |
パラメーターの名前を取得または設定します。 |
| Size |
パラメーターのサイズを取得または設定します。 |
| Type |
パラメーターの型を取得または設定します。 |
| ViewState |
同じページに対する複数の要求にわたって、 Parameter オブジェクトのビュー ステートを保存および復元できる状態情報のディクショナリを取得します。 |
メソッド
| 名前 | 説明 |
|---|---|
| Clone() |
現在の Parameter インスタンスの複製を返します。 |
| ConvertDbTypeToTypeCode(DbType) | |
| ConvertTypeCodeToDbType(TypeCode) | |
| Equals(Object) |
指定したオブジェクトが現在のオブジェクトと等しいかどうかを判断します。 (継承元 Object) |
| Evaluate(HttpContext, Control) |
Parameter オブジェクトの値を更新して返します。 |
| GetDatabaseType() | |
| GetHashCode() |
既定のハッシュ関数として機能します。 (継承元 Object) |
| GetType() |
現在のインスタンスの Type を取得します。 (継承元 Object) |
| LoadViewState(Object) |
データ ソース ビューの以前に保存したビューステートを復元します。 |
| MemberwiseClone() |
現在の Objectの簡易コピーを作成します。 (継承元 Object) |
| OnParameterChanged() |
Parameter オブジェクトを含むParameterCollection コレクションのOnParametersChanged(EventArgs) メソッドを呼び出します。 |
| SaveViewState() |
ページがサーバーにポストバックされてからの Parameter オブジェクトのビューステートへの変更を保存します。 |
| SetDirty() |
Parameter オブジェクトの状態がビュー ステートに記録されるようにマークします。 |
| ToString() |
このインスタンスの値を等価の文字列形式に変換します。 |
| TrackViewState() |
Parameter オブジェクトがビューステートの変更を追跡し、コントロールのViewState オブジェクトに格納し、同じページに対する要求間で永続化できるようにします。 |
明示的なインターフェイスの実装
| 名前 | 説明 |
|---|---|
| ICloneable.Clone() |
現在の Parameter インスタンスの複製を返します。 |
| IStateManager.IsTrackingViewState |
Parameter オブジェクトがビュー ステートへの変更を保存しているかどうかを示す値を取得します。 |
| IStateManager.LoadViewState(Object) |
データ ソース ビューの以前に保存したビューステートを復元します。 |
| IStateManager.SaveViewState() |
ページがサーバーにポストバックされてからの Parameter オブジェクトのビューステートへの変更を保存します。 |
| IStateManager.TrackViewState() |
Parameter オブジェクトがビューステートの変更を追跡し、コントロールのViewState オブジェクトに格納し、同じページに対する要求間で永続化できるようにします。 |