QueryStringParameter 類別

定義

將 HTTP 要求查詢字串欄位的值繫結至參數物件。

public ref class QueryStringParameter : System::Web::UI::WebControls::Parameter
public class QueryStringParameter : System.Web.UI.WebControls.Parameter
type QueryStringParameter = class
    inherit Parameter
Public Class QueryStringParameter
Inherits Parameter
繼承
QueryStringParameter

範例

下列範例示範如何在控制項中 GridView 顯示資料時,建立 QueryStringParameter 物件做為篩選。 您可以將 物件新增 QueryStringParameterAccessDataSource 控制項的 FilterParameters 集合。 參數物件會將名為 country 的查詢字串欄位值系結至其 FilterExpression 字串。 因為參數沒有 DefaultValue 指定任何屬性,所以如果沒有以查詢字串傳遞名為 country 的欄位,控制項 AccessDataSource 就會 NullReferenceException 擲回例外狀況。 如果已傳遞名為 country 的欄位,但沒有值,控制項 GridView 就不會顯示任何資料。

<%@ Page language="C#"%>
<!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" method="post" runat="server">

      <!-- Use a Query String with country=USA -->
      <asp:gridview
        id ="GridView1"
        runat="server"
        datasourceid="MyAccessDataSource" />

<!-- Security Note: The AccessDataSource uses a QueryStringParameter,
     Security Note: which does not perform validation of input from the client. -->

      <asp:accessdatasource
        id="MyAccessDataSource"
        runat="server"
        datafile="Northwind.mdb"
        selectcommand="SELECT EmployeeID, LastName, Address, PostalCode, Country FROM Employees"
        filterexpression="Country = '{0}'">
        <filterparameters>
          <asp:querystringparameter name="country" type="String" querystringfield="country" />
        </filterparameters>
      </asp:accessdatasource>
    </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">

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="Form1" method="post" runat="server">

      <!-- Use a Query String with country=USA -->
      <asp:gridview
        id ="GridView1"
        runat="server"
        datasourceid="MyAccessDataSource" />

<!-- Security Note: The AccessDataSource uses a QueryStringParameter,
     Security Note: which does not perform validation of input from the client. -->

      <asp:accessdatasource
        id="MyAccessDataSource"
        runat="server"
        datafile="Northwind.mdb"
        selectcommand="SELECT EmployeeID, LastName, Address, PostalCode, Country FROM Employees"
        filterexpression="Country = '{0}'">
        <filterparameters>
          <asp:querystringparameter name="country" type="String" querystringfield="country" />
        </filterparameters>
      </asp:accessdatasource>

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

下列範例示範如何使用參數化 SQL 查詢, QueryStringParameter 建立 物件以顯示 Access 資料庫的資料。 物件 AccessDataSource 會擷取接著顯示在 控制項中的 GridView 記錄。 控制項 GridView 也可以編輯,並可讓使用者更新 Northwind Traders Orders 資料表中的訂單狀態。

<%@Page  Language="C#" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<!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 UpdateRecords(Object source, EventArgs e)
{
  // This method is an example of batch updating using a
  // data source control. The method iterates through the rows
  // of the GridView, extracts each CheckBox from the row and, if
  // the CheckBox is checked, updates data by calling the Update
  // method of the data source control, adding required parameters
  // to the UpdateParameters collection.
  CheckBox cb;
  foreach(GridViewRow row in this.GridView1.Rows) {
    cb = (CheckBox) row.Cells[0].Controls[1];
    if(cb.Checked) {
      string oid = (string) row.Cells[1].Text;
      MyAccessDataSource.UpdateParameters.Add(new Parameter("date",TypeCode.DateTime,DateTime.Now.ToString()));
      MyAccessDataSource.UpdateParameters.Add(new Parameter("orderid",TypeCode.String,oid));
      MyAccessDataSource.Update();
      MyAccessDataSource.UpdateParameters.Clear();
    }
  }
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

<!-- Security Note: The SqlDataSource uses a QueryStringParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the QueryStringParameter, handle the Selecting event. -->

      <asp:SqlDataSource
        id="MyAccessDataSource"
        runat="server"
        ProviderName="<%$ ConnectionStrings:MyPasswordProtectedAccess.providerName%>"
        ConnectionString="<%$ ConnectionStrings:MyPasswordProtectedAccess%>"
        SelectCommand="SELECT OrderID, OrderDate, RequiredDate, ShippedDate FROM Orders WHERE EmployeeID=?"
        UpdateCommand="UPDATE Orders SET ShippedDate=? WHERE OrderID = ?">
        <SelectParameters>
          <asp:QueryStringParameter Name="empId" QueryStringField="empId" />
        </SelectParameters>
      </asp:SqlDataSource>

      <asp:GridView
        id ="GridView1"
        runat="server"
        DataSourceID="MyAccessDataSource"
        AllowPaging="True"
        PageSize="10"
        AutoGenerateColumns="False">
          <columns>
            <asp:TemplateField HeaderText="">
              <ItemTemplate>
                <asp:CheckBox runat="server" />
              </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Order" DataField="OrderID" />
            <asp:BoundField HeaderText="Order Date" DataField="OrderDate" />
            <asp:BoundField HeaderText="Required Date" DataField="RequiredDate" />
            <asp:BoundField HeaderText="Shipped Date" DataField="ShippedDate" />
          </columns>
      </asp:GridView>

      <asp:Button
        id="Button1"
        runat="server"
        Text="Update the Selected Records As Shipped"
        OnClick="UpdateRecords" />

      <asp:Label id="Label1" runat="server" />

    </form>
  </body>
</html>
<%@Page  Language="VB" %>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.Common" %>
<!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 UpdateRecords(source As Object, e As EventArgs)

  ' This method is an example of batch updating using a
  ' data source control. The method iterates through the rows
  ' of the GridView, extracts each CheckBox from the row and, if
  ' the CheckBox is checked, updates data by calling the Update
  ' method of the data source control, adding required parameters
  ' to the UpdateParameters collection.

  Dim cb As CheckBox
  Dim row As GridViewRow

  For Each row In GridView1.Rows

    cb = CType(row.Cells(0).Controls(1), CheckBox)
    If cb.Checked Then

      Dim oid As String
      oid = CType(row.Cells(1).Text, String)

      Dim param1 As New Parameter("date", TypeCode.DateTime, DateTime.Now.ToString())
      MyAccessDataSource.UpdateParameters.Add(param1)

      Dim param2 As New Parameter("orderid", TypeCode.String, oid)
      MyAccessDataSource.UpdateParameters.Add(param2)

      MyAccessDataSource.Update()
      MyAccessDataSource.UpdateParameters.Clear()
    End If
  Next
End Sub ' UpdateRecords
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">

<!-- Security Note: The SqlDataSource uses a QueryStringParameter,
     Security Note: which does not perform validation of input from the client.
     Security Note: To validate the value of the QueryStringParameter, handle the Selecting event. -->

      <asp:SqlDataSource
        id="MyAccessDataSource"
        runat="server"
        ProviderName="<%$ ConnectionStrings:MyPasswordProtectedAccess.providerName%>"
        ConnectionString="<%$ ConnectionStrings:MyPasswordProtectedAccess%>"
        SelectCommand="SELECT OrderID, OrderDate, RequiredDate, ShippedDate FROM Orders WHERE EmployeeID=?"
        UpdateCommand="UPDATE Orders SET ShippedDate=? WHERE OrderID = ?">
        <SelectParameters>
          <asp:QueryStringParameter Name="empId" QueryStringField="empId" />
        </SelectParameters>
      </asp:SqlDataSource>

      <asp:GridView
        id ="GridView1"
        runat="server"
        DataSourceID="MyAccessDataSource"
        AllowPaging="True"
        PageSize="10"
        AutoGenerateColumns="False">
          <columns>
            <asp:TemplateField HeaderText="">
              <ItemTemplate>
                <asp:CheckBox runat="server" />
              </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Order" DataField="OrderID" />
            <asp:BoundField HeaderText="Order Date" DataField="OrderDate" />
            <asp:BoundField HeaderText="Required Date" DataField="RequiredDate" />
            <asp:BoundField HeaderText="Shipped Date" DataField="ShippedDate" />
          </columns>
      </asp:GridView>

      <asp:Button
        id="Button1"
        runat="server"
        Text="Update the Selected Records As Shipped"
        OnClick="UpdateRecords" />

      <asp:Label id="Label1" runat="server" />

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

備註

您可以使用 QueryStringParameter 類別,將做為 HTTP 要求查詢字串一部分傳遞的欄位值系結至參數化查詢或命令中使用的參數。 欄位是從集合中 QueryString 擷取的。

系結資料至 參數的控制項可能會在參考物件時 QueryStringParameter 擲回例外狀況,但不會傳遞對應的查詢字串名稱/值組。 同樣地,如果在沒有對應值的情況下傳遞查詢字串功能變數名稱,它們可能不會顯示任何資料。 若要避免這些情況,請適當地設定 DefaultValue 屬性。

類別 QueryStringParameter 提供 QueryStringField 屬性,可識別要系結的查詢字串值名稱。 它也提供繼承自 Parameter 類別的屬性。

重要

類別 QueryStringParameter 不會驗證傳遞的值;它提供原始值。 不過,您可以在資料來源控制項中驗證 物件的值 QueryStringParameter 。 若要這樣做,請處理 Selecting 資料來源控制項的 、 UpdatingInsertingDeleting 事件,並檢查事件處理常式中的參數值。 如果 參數的值未通過驗證測試,您可以將相關聯 CancelEventArgs 類別的 屬性設定 Canceltrue 來取消資料作業。

建構函式

QueryStringParameter()

初始化 QueryStringParameter 類別未命名的新執行個體。

QueryStringParameter(QueryStringParameter)

初始化 QueryStringParameter 類別的新執行個體,使用由 original 參數指定的執行個體值。

QueryStringParameter(String, DbType, String)

使用指定的查詢字串欄位和參數的資料型別,初始化 QueryStringParameter 類別的新具名執行個體。

QueryStringParameter(String, String)

使用指定的字串識別要繫結的查詢字串欄位,初始化 QueryStringParameter 類別的新具名執行個體。

QueryStringParameter(String, TypeCode, String)

使用指定的字串識別要繫結的查詢字串欄位,初始化 QueryStringParameter 類別的新具名強型別 (Strongly Typed) 執行個體。

屬性

ConvertEmptyStringToNull

取得或設定值,該值指示是否應將 Parameter 物件繫結至的值轉換成 null (如果其為 Empty)。

(繼承來源 Parameter)
DbType

取得或設定參數的資料庫型別。

(繼承來源 Parameter)
DefaultValue

指定參數的預設值,當呼叫 Evaluate(HttpContext, Control) 方法時,要繫結的值應是此參數未初始化的值。

(繼承來源 Parameter)
Direction

表示此 Parameter 物件是否用來將值繫結至控制項,或是這個控制項是否可用來變更該值。

(繼承來源 Parameter)
IsTrackingViewState

取得值,指出 Parameter 物件是否正在將變更儲存到它的檢視狀態。

(繼承來源 Parameter)
Name

取得或設定參數的名稱。

(繼承來源 Parameter)
QueryStringField

取得或設定參數所繫結至的查詢字串欄位名稱。

Size

取得或設定參數的大小。

(繼承來源 Parameter)
Type

取得或設定參數的類型。

(繼承來源 Parameter)
ValidateInput

取得或設定是否正在驗證查詢字串參數的值。

ViewState

取得狀態資訊的字典,允許您在相同頁面的多個要求之間,儲存和還原 Parameter 物件的檢視狀態。

(繼承來源 Parameter)

方法

Clone()

傳回目前 QueryStringParameter 執行個體的複製。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Evaluate(HttpContext, Control)

更新並傳回 QueryStringParameter 物件的值。

GetDatabaseType()

取得 DbType 值,該值等於目前 Parameter 執行個體的 CLR 型別。

(繼承來源 Parameter)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
LoadViewState(Object)

將資料來源檢視還原成之前所儲存的檢視狀態。

(繼承來源 Parameter)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnParameterChanged()

呼叫 OnParametersChanged(EventArgs) 集合的 ParameterCollection 方法,該集合包含給定的 Parameter 物件。

(繼承來源 Parameter)
SaveViewState()

儲存自頁面回傳至伺服器以來 Parameter 物件檢視狀態的變更。

(繼承來源 Parameter)
SetDirty()

標記 Parameter 物件,以便將其狀態記錄在檢視狀態中。

(繼承來源 Parameter)
ToString()

將這個執行個體的值轉換為它的相等字串表示。

(繼承來源 Parameter)
TrackViewState()

會造成 Parameter 物件追蹤其檢視狀態變更,以將這些變更儲存在控制項的 ViewState 物件中,並持續存取相同頁面的其他要求。

(繼承來源 Parameter)

明確介面實作

ICloneable.Clone()

傳回目前 Parameter 執行個體的複製。

(繼承來源 Parameter)
IStateManager.IsTrackingViewState

取得值,指出 Parameter 物件是否正在將變更儲存到它的檢視狀態。

(繼承來源 Parameter)
IStateManager.LoadViewState(Object)

將資料來源檢視還原成之前所儲存的檢視狀態。

(繼承來源 Parameter)
IStateManager.SaveViewState()

儲存自頁面回傳至伺服器以來 Parameter 物件檢視狀態的變更。

(繼承來源 Parameter)
IStateManager.TrackViewState()

會造成 Parameter 物件追蹤其檢視狀態變更,以將這些變更儲存在控制項的 ViewState 物件中,並持續存取相同頁面的其他要求。

(繼承來源 Parameter)

適用於

另請參閱