ParameterCollection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
public ref class ParameterCollection : System::Web::UI::StateManagedCollection
public class ParameterCollection : System.Web.UI.StateManagedCollection
type ParameterCollection = class
inherit StateManagedCollection
Public Class ParameterCollection
Inherits StateManagedCollection
- 繼承
範例
下列程式碼範例示範如何使用 AccessDataSource 控制項和 , FormParameter 在 控制項中 GridView 顯示 Microsoft Access 資料庫中的資訊。 物件 FormParameter 會使用 Add 方法新增至 SelectParameters 集合。
重要
這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
<%@Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load(Object sender, EventArgs e){
// You can add a FormParameter to the AccessDataSource control's
// SelectParameters collection programmatically.
AccessDataSource1.SelectParameters.Clear();
// Security Note: The AccessDataSource uses a FormParameter,
// Security Note: which does not perform validation of input from the client.
// Security Note: To validate the value of the FormParameter,
// Security Note: handle the Selecting event.
FormParameter formParam = new FormParameter("lastname","LastNameBox");
formParam.Type=TypeCode.String;
AccessDataSource1.SelectParameters.Add(formParam);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:accessdatasource
id="AccessDataSource1"
runat="server"
datasourcemode="DataSet"
datafile="Northwind.mdb"
selectcommand="SELECT OrderID,CustomerID,OrderDate,RequiredDate,ShippedDate
FROM Orders WHERE EmployeeID =
(SELECT EmployeeID FROM Employees WHERE LastName = @lastname)">
</asp:accessdatasource>
<br />Enter the name "Davolio" or "King" in the text box and click the button.
<br />
<asp:textbox
id="LastNameBox"
runat="server" />
<br />
<asp:button
id="Button1"
runat="server"
text="Get Records" />
<br />
<asp:gridview
id="GridView1"
runat="server"
allowsorting="True"
datasourceid="AccessDataSource1">
</asp:gridview>
</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">
<script runat="server">
Private Sub Page_Load(sender As Object, e As EventArgs)
' You can add a FormParameter to the AccessDataSource control's
' SelectParameters collection programmatically.
AccessDataSource1.SelectParameters.Clear()
' Security Note: The AccessDataSource uses a FormParameter,
' Security Note: which does not perform validation of input from the client.
' Security Note: To validate the value of the FormParameter,
' Security Note: handle the Selecting event.
Dim formParam As New FormParameter("lastname","LastNameBox")
formParam.Type=TypeCode.String
AccessDataSource1.SelectParameters.Add(formParam)
End Sub ' Page_Load
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:accessdatasource
id="AccessDataSource1"
runat="server"
datasourcemode="DataSet"
datafile="Northwind.mdb"
selectcommand="SELECT OrderID,CustomerID,OrderDate,RequiredDate,ShippedDate
FROM Orders WHERE EmployeeID =
(SELECT EmployeeID FROM Employees WHERE LastName = @lastname)">
</asp:accessdatasource>
<br />Enter the name "Davolio" or "King" in the text box and click the button.
<br />
<asp:textbox
id="LastNameBox"
runat="server" />
<br />
<asp:button
id="Button1"
runat="server"
text="Get Records" />
<br />
<asp:gridview
id="GridView1"
runat="server"
allowsorting="True"
datasourceid="AccessDataSource1">
</asp:gridview>
</form>
</body>
</html>
備註
類別 ParameterCollection 代表物件集合,這些物件用於具有資料來源控制項的 Parameter 進階資料系結案例中。 物件 Parameter 可用來在擷取、更新、刪除和插入資料時,將局部 Page 變數、HTTP Cookie、會話變數和其他控制項值所包含的值系結至資料來源控制項。
使用 類別 ParameterCollection 以程式設計方式管理一組 Parameter 物件。 您可以使用 類別的適當方法 ParameterCollection 來新增、插入和移除 Parameter 物件。 若要以程式設計方式從集合擷取 Parameter 物件,請使用下列其中一種方法:
使用索引子,依名稱或使用陣列標記法,從集合中取得單 Parameter 一物件。
GetEnumerator使用 方法來建立 System.Collections.IEnumerator 實作的物件,然後可用來從集合取得專案。
屬性 Count 會指定集合中的專案總數,並用來判斷集合的上限。 您可以使用 、 Insert 、 Remove 和 RemoveAt 方法,從集合 Add 新增和移除專案。
根據特定資料來源控制項的實作和語意,參數儲存在 ParameterCollection 集合中的順序可能很重要。 例如,使用 SqlDataSource 控制項做為 ODBC 資料來源時,集合中 ParameterCollection 物件的順序 Parameter 必須與您使用的參數化 SQL 查詢中的參數順序相同。 不過,搭配 Microsoft SQL Server 使用 SqlDataSource 控制項時,物件的順序 Parameter 並不重要。
重要
當您使用資料來源控制項時,值會插入命令參數中,而不需要驗證,這是潛在的安全性威脅。 使用資料來源控制項中的 事件,在執行命令之前先驗證參數值。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。
下表列出不同的參數類別及其使用方式。
Parameter 類別 | 描述 |
---|---|
Parameter | 基底參數類別。 使用它來系結至區域變數或任何使用 屬性的 DefaultValue 靜態字串。 |
ControlParameter | 參數,可用來系結至控制項的屬性或方法傳回值。 |
CookieParameter | 參數,可用來系結至 Cookie 的值。 |
FormParameter | 參數,可用來系結至目前Web Form頁面的屬性。 |
QueryStringParameter | 參數,可用來系結至查詢字串上傳遞至Web Form頁面的值。 |
SessionParameter | 參數,可用來系結至會話變數的值。 |
ProfileParameter | 參數,可用來系結至 ASP.NET Profile 屬性的值。 |
建構函式
ParameterCollection() |
初始化這個類別,以供繼承的類別執行個體使用。 此建構函式只能由繼承的類別呼叫。 |
屬性
Count |
取得 StateManagedCollection 集合中所包含的項目數。 (繼承來源 StateManagedCollection) |
Item[Int32] |
取得或設定位於集合中所指定索引處的 Parameter 物件。 |
Item[String] |
取得或設定集合中具有指定名稱的 Parameter 物件。 |
方法
事件
ParametersChanged |
當集合所包含的一個或多個 Parameter 物件變更狀態時發生。 |
明確介面實作
擴充方法
Cast<TResult>(IEnumerable) |
將 IEnumerable 的項目轉換成指定的型別。 |
OfType<TResult>(IEnumerable) |
根據指定的型別來篩選 IEnumerable 的項目。 |
AsParallel(IEnumerable) |
啟用查詢的平行化作業。 |
AsQueryable(IEnumerable) |
將 IEnumerable 轉換成 IQueryable。 |
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應