Parameter Osztály
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Olyan mechanizmust biztosít, amellyel az adatforrás-vezérlők alkalmazásváltozókhoz, felhasználói identitásokhoz és választási lehetőségekhez és egyéb adatokhoz kötődnek. Az összes ASP.NET paramétertípus alaposztályaként szolgál.
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
- Öröklődés
-
Parameter
- Származtatott
- Megvalósítás
Példák
Az alábbi példa bemutatja, hogyan használhatja DropDownList egy vezérlőelem kiválasztott értékét egy SQL-lekérdezés Where záradékában. A példa az ControlParameter osztályból ControlParameter származó osztályt használja.
Az SelectCommand elem egy "@Title" nevű paraméterrel definiálja a lekérdezést, ahol az értéknek DropDownList1 el kell indulnia. Az ControlParameter elem azt határozza meg, hogy a "@Title" helyőrző helyébe a vezérlőelem tulajdonságának SelectedValueDropDownList1 értéke lép. Az ControlParameter elem hozzá lesz adva a SelectParameters vezérlő gyűjteményéhez 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>
Az alábbi példa az előzőhez hasonló, de kóddal, korrektúra helyett használ. Amikor az oldal első alkalommal töltődik be, a DropDownList vezérlőelem nem rendelkezik kijelölt értékkel, és az DefaultValueParameter objektum tulajdonságát használja a rendszer.
<%@ 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>
Az alábbi kód az előző példában az oldal mögötti osztályt mutatja be.
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
Az alábbi példakód bemutatja, hogyan terjeszthető ki az Parameter osztály egy új paramétertípus létrehozására, amelyet az adatforrás-vezérlők és az adatkötési forgatókönyvek egyéb vezérlői használhatnak. Az adatforrás-vezérlők egy StaticParameter paraméterrel bármely, általában egy webűrlapon deklarált objektum, általában egy sztring értékéhez kötődhetnek.
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
Megjegyzések
A Parameter osztály egy paraméteres SQL-lekérdezés, egy szűrőkifejezés vagy egy üzleti objektum metódushívás paraméterét jelöli, amelyet egy ASP.NET adatforrás-vezérlő az adatok kiválasztására, szűrésére vagy módosítására használ. Parameter objektumok egy objektumban ParameterCollection találhatók. Parameter az objektumok kiértékelése futásidőben történik, hogy az általuk képviselt változók értékeit az adatforrás-vezérlő által az adatokkal való interakcióhoz használt bármely módszerhez kösse.
Webalapú adatalkalmazások létrehozásához használjon adatforrásból és adathoz kötött vezérlőkből származó Parameter osztályokat. Ezeket a paraméterosztályokat az adatforrás-vezérlők arra használják, hogy a webalkalmazásokban található bizonyos típusú értékeket az SQL-lekérdezési sztringek helyőrzőihez, az üzleti objektum metódus paramétereihez stb. Az alábbi táblázat a ASP.NET részét képező paramétertípusokat sorolja fel.
| Paraméter típusa | Leírás |
|---|---|
| ControlParameter | Egy webkiszolgáló-vezérlő bármely nyilvános tulajdonságát köti össze. |
| FormParameter | Űrlapmező kötése. |
| SessionParameter | Munkamenet-állapot mező kötése. |
| RouteParameter | Egy útvonal URL-paraméterének kötése. |
| CookieParameter | Cookie-mező kötése. |
| QueryStringParameter | Egy lekérdezési sztring paraméter kötése. |
| ProfileParameter | Profilmező kötése. |
Bővítse ki az alaposztályt Parameter , ha saját egyéni paramétertípusokat szeretne implementálni.
Parameter Az objektumok nagyon egyszerűek: rendelkeznek egy és egy NameType tulajdonságmal, deklaratív módon ábrázolhatók, és több HTTP-kérésben is nyomon követhetik az állapotot. Minden paraméter támogatja a tulajdonságot DefaultValue olyan esetekben, amikor egy paraméter egy értékhez van kötve, de az érték futásidőben kiértékelhető null .
Ha adatforrás-vezérlővel rendelkező objektumgyűjteményt Parameter használ, a gyűjteményben való sorrendjük számíthat. További információ a paraméterek használatáról: Paraméterek használata az SqlDataSource-vezérlővel és paraméterek használata az ObjectDataSource-vezérlővel.
Konstruktorok
| Név | Description |
|---|---|
| Parameter() |
Inicializálja az osztály új alapértelmezett példányát Parameter . |
| Parameter(Parameter) |
Inicializálja az Parameter osztály új példányát az eredeti, megadott példány értékeivel. |
| Parameter(String, DbType, String) |
Inicializálja az Parameter osztály új példányát a megadott név, a megadott adatbázistípus és a tulajdonság megadott értéke DefaultValue alapján. |
| Parameter(String, DbType) |
Inicializálja az Parameter osztály új példányát a megadott név és adatbázistípus használatával. |
| Parameter(String, TypeCode, String) |
Inicializálja az Parameter osztály új példányát a megadott név, a megadott típus és a tulajdonsághoz DefaultValue megadott sztring használatával. |
| Parameter(String, TypeCode) |
Inicializálja az osztály új példányát Parameter a megadott név és típus használatával. |
| Parameter(String) |
Inicializálja az Parameter osztály új példányát a megadott névvel. |
Tulajdonságok
| Név | Description |
|---|---|
| ConvertEmptyStringToNull |
Lekéri vagy beállít egy értéket, amely jelzi, hogy az Parameter objektumhoz kötött értéket konvertálni |
| DbType |
Lekéri vagy beállítja a paraméter adatbázistípusát. |
| DefaultValue |
A paraméter alapértelmezett értékét adja meg, ha a metódus meghívásakor Evaluate(HttpContext, Control) a paraméter nem inicializálódik. |
| Direction |
Azt jelzi, hogy az Parameter objektum egy vezérlőelemhez köti-e az értéket, vagy a vezérlőelem használható az érték módosítására. |
| IsTrackingViewState |
Beolvas egy értéket, amely jelzi, hogy az Parameter objektum menti-e a nézetállapot módosításait. |
| Name |
Lekéri vagy beállítja a paraméter nevét. |
| Size |
Lekéri vagy beállítja a paraméter méretét. |
| Type |
Lekéri vagy beállítja a paraméter típusát. |
| ViewState |
Lekéri az állapotinformációk szótárát, amely lehetővé teszi egy objektum nézetállapotának mentését és visszaállítását Parameter ugyanazon lap több kérésében. |
Metódusok
| Név | Description |
|---|---|
| Clone() |
Az aktuális Parameter példány duplikátumát adja vissza. |
| ConvertDbTypeToTypeCode(DbType) | |
| ConvertTypeCodeToDbType(TypeCode) | |
| Equals(Object) |
Meghatározza, hogy a megadott objektum egyenlő-e az aktuális objektummal. (Öröklődés forrása Object) |
| Evaluate(HttpContext, Control) |
Frissíti és visszaadja az Parameter objektum értékét. |
| GetDatabaseType() |
Lekéri az DbType aktuális Parameter példány CLR-típusával egyenértékű értéket. |
| GetHashCode() |
Ez az alapértelmezett kivonatoló függvény. (Öröklődés forrása Object) |
| GetType() |
Lekéri az Type aktuális példányt. (Öröklődés forrása Object) |
| LoadViewState(Object) |
Visszaállítja az adatforrásnézet korábban mentett nézetállapotát. |
| MemberwiseClone() |
Az aktuális Objectpéldány sekély másolatát hozza létre. (Öröklődés forrása Object) |
| OnParameterChanged() |
Meghívja OnParametersChanged(EventArgs) az ParameterCollection objektumot tartalmazó gyűjtemény metódusátParameter. |
| SaveViewState() |
Menti az Parameter objektum nézetállapotának módosításait a lap kiszolgálóra való visszatérése óta. |
| SetDirty() |
Megjelöli az Parameter objektumot, hogy az állapota megtekintési állapotban legyen rögzítve. |
| ToString() |
A példány értékét a megfelelő sztring-ábrázolásúvá alakítja. |
| TrackViewState() |
Ez azt eredményezi, hogy az Parameter objektum nyomon követi a nézetállapot változásait, hogy azok a vezérlő ViewState objektumában tárolhatók legyenek, és ugyanazon lap kérései között megmaradjanak. |
Explicit interfész-implementációk
| Név | Description |
|---|---|
| ICloneable.Clone() |
Az aktuális Parameter példány duplikátumát adja vissza. |
| IStateManager.IsTrackingViewState |
Beolvas egy értéket, amely jelzi, hogy az Parameter objektum menti-e a nézetállapot módosításait. |
| IStateManager.LoadViewState(Object) |
Visszaállítja az adatforrásnézet korábban mentett nézetállapotát. |
| IStateManager.SaveViewState() |
Menti az Parameter objektum nézetállapotának módosításait a lap kiszolgálóra való visszatérése óta. |
| IStateManager.TrackViewState() |
Ez azt eredményezi, hogy az Parameter objektum nyomon követi a nézetállapot változásait, hogy azok a vezérlő ViewState objektumában tárolhatók legyenek, és ugyanazon lap kérései között megmaradjanak. |