更新 : 2007 年 11 月
アプリケーション変数、ユーザー ID とユーザー選択、および他のデータにバインドするためにデータ ソース コントロールで使用する機構を提供します。ASP.NET のすべてのパラメータ型の基本クラスとして機能します。
名前空間 :
System.Web.UI.WebControls アセンブリ :
System.Web (System.Web.dll 内)
<AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level := AspNetHostingPermissionLevel.Minimal)> _
Public Class Parameter _
Implements ICloneable, IStateManager
Dim instance As Parameter
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class Parameter : ICloneable, IStateManager
[AspNetHostingPermissionAttribute(SecurityAction::InheritanceDemand, Level = AspNetHostingPermissionLevel::Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction::LinkDemand, Level = AspNetHostingPermissionLevel::Minimal)]
public ref class Parameter : ICloneable,
IStateManager
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal) */
/** @attribute AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal) */
public class Parameter implements ICloneable,
IStateManager
public class Parameter implements ICloneable, IStateManager
Parameter クラスは、ASP.NET データ ソース コントロールがデータの選択、フィルタ処理、変更に使用するパラメータ化された SQL クエリ、フィルタ式、またはビジネス オブジェクト メソッド呼び出しのパラメータを表します。Parameter オブジェクトは、ParameterCollection オブジェクトに格納されます。Parameter オブジェクトは実行時に評価され、それらのオブジェクトが表す変数の値をデータ ソース コントロールで使用するメソッドにバインドしてデータとやり取りします。
ControlParameter、CookieParameter、SessionParameter、ProfileParameter、QueryStringParameter など、ASP.NET に提供されるパラメータ クラスは、Web ベースのデータ アプリケーションをビルドするデータ ソース コントロールおよびデータ バインド コントロールで使用します。これらのクラスはデータ ソース コントロールで使用され、Web アプリケーション内の特定の種類の値を、SQL クエリ文字列やビジネス オブジェクト メソッドのパラメータなどに含まれるプレースホルダにバインドします。たとえば、ControlParameter クラスは Web サーバー コントロールのパブリック プロパティをバインドするために使用され、SessionParameter クラスはユーザー セッションの値をバインドするために使用されます。また、QueryStringParameter クラスと CookieParameter クラスは、HttpRequest クラスの値にバインドするために使用されます。独自のカスタム パラメータ型を実装する場合は、基本 Parameter クラスを拡張します。
Parameter オブジェクトは非常に簡単なオブジェクトです。このオブジェクトには、Name プロパティと Type プロパティがあります。このオブジェクトは、宣言によって表すことができ、複数の HTTP 要求にわたり状態を追跡できます。パラメータが値にバインドされたときのために、すべてのパラメータは DefaultValue プロパティをサポートしていますが、この値は実行時に nullNothingnullptrnull 参照 (Visual Basic では Nothing) と評価されます。
データ ソース コントロールで Parameter オブジェクトのコレクションを使用する場合、コレクション内でのオブジェクトの順序が重要となります。パラメータが使用されるしくみの詳細については、「SqlDataSource コントロールにおけるパラメータの使用」および「ObjectDataSource コントロールにおけるパラメータの使用」を参照してください。
宣言シナリオで、Parameter オブジェクトを使用して、ListBox コントロールに表示されたデータを DropDownList コントロールの選択された値にバインドする方法を次のコード例に示します。ControlParameter オブジェクトは、フォームの SqlDataSource コントロールの SelectParameters コレクションに追加され、SelectCommand プロパティの "@Title" プレースホルダ テキストに対応します。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<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 >
<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 >
<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="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;"
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>
プログラム シナリオで、Parameter オブジェクトを使用して、DataGrid コントロールに表示されたデータを DropDownList コントロールの選択された値にバインドするコード例を次に示します。ページが最初に読み込まれたときには、DropDownList コントロールの値は選択されていないため、Parameter オブジェクトの DefaultValue プロパティが使用されます。
<%@ 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 >
<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="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 >
<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="VJ#" %>
<!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 Page_Load(Object sender, System.EventArgs e)
{
SqlDataSource sqlSource = new SqlDataSource("Data Source=localhost;"
+ "Integrated Security=SSPI;Initial Catalog=Northwind;",
"SELECT FirstName, LastName FROM Employees WHERE Country = @country;");
ControlParameter country = new ControlParameter();
country.set_Name("country");
country.set_Type(System.TypeCode.String);
country.set_ControlID("DropDownList1");
country.set_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.set_DefaultValue("USA");
sqlSource.get_SelectParameters().Add(country);
// Add the SqlDataSource to the page controls collection.
get_Page().get_Controls().Add(sqlSource);
DataGrid1.set_DataSource(sqlSource);
DataGrid1.DataBind();
} //Page_Load
</script>
<html >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<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" />
</form>
</body>
</html>
次の分離コード モジュールは、前の Web フォーム ページで使用されます。
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 'Page_Load
End Class
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();
}
}
Parameter クラスを拡張して、データ バインディング シナリオでデータ ソース コントロールと他のコントロールで使用できる新しいパラメータ型を作成する方法を次のコード例に示します。データ ソース コントロールは、StaticParameter パラメータを使用して、Web フォーム ページ上で宣言されたオブジェクトの値 (通常は文字列) にバインドできます。
Imports System
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 ' StaticParameter
End Namespace ' Samples.AspNet
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;
}
}
}
package Samples.AspNet ;
import System.* ;
import System.ComponentModel.* ;
import System.Web.UI.* ;
import System.Web.UI.WebControls.* ;
public class StaticParameter extends Parameter
{
public StaticParameter()
{
} //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)
{
super(name);
set_DataValue(value);
} //StaticParameter
// 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)
{
super(name, type);
set_DataValue(value);
} //StaticParameter
// 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)
{
super(original);
set_DataValue(original.get_DataValue());
} //StaticParameter
// 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 Parameter Clone()
{
return new StaticParameter(this);
} //Clone
// The DataValue can be any arbitrary object and is stored in ViewState.
/** @property
*/
public Object get_DataValue()
{
return get_ViewState().get_Item("Value");
} //get_DataValue
/** @property
*/
public void set_DataValue(Object value)
{
get_ViewState().set_Item("Value", value);
} //set_DataValue
// 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.
/** @property
*/
public String get_Value()
{
Object o = get_DataValue();
if (o == null || !(o instanceof String)) {
return String.Empty;
}
return (String)(o);
} //get_Value
/** @property
*/
public void set_Value(String value)
{
set_DataValue(value);
OnParameterChanged();
} //set_Value
// The Evaluate method is overridden to return the
// DataValue property instead of the DefaultValue.
protected Object Evaluate(Control control)
{
return get_DataValue();
} //Evaluate
} //StaticParameter
System..::.Object
System.Web.UI.WebControls..::.Parameter
System.Web.UI.WebControls..::.ControlParameter
System.Web.UI.WebControls..::.CookieParameter
System.Web.UI.WebControls..::.FormParameter
System.Web.UI.WebControls..::.ProfileParameter
System.Web.UI.WebControls..::.QueryStringParameter
System.Web.UI.WebControls..::.SessionParameter
この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
.NET Framework
サポート対象 : 3.5、3.0、2.0
参照
その他の技術情報