QueryStringParameter 類別

定義

將 HTTP 要求查詢字串欄位的值繫結至參數物件。Binds the value of an HTTP request query-string field to a parameter object.

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

範例

下列範例顯示 QueryStringParameter 當您在控制項中顯示資料時,如何建立物件做為篩選準則 GridViewThe following example shows how to create a QueryStringParameter object to use as a filter when you display data in a GridView control. 您可以將 QueryStringParameter 物件加入 AccessDataSource 控制項的 FilterParameters 集合中。You add the QueryStringParameter object to the AccessDataSource control's FilterParameters collection. 參數物件會將名為之查詢字串欄位的值系結 country 至其 FilterExpression 字串。The parameter object binds the value of the query-string field named country to its FilterExpression string. 因為沒有 DefaultValue 指定參數的屬性,所以如果沒有任何名為的欄位 country 與查詢字串一起傳遞,控制項就會擲回 AccessDataSource NullReferenceException 例外狀況。Because no DefaultValue property is specified for the parameter, if no field named country is passed with the query string, the AccessDataSource control throws a NullReferenceException exception. 如果傳遞了名為的欄位 country ,但沒有任何值, GridView 控制項就不會顯示任何資料。If a field named country is passed but has no value, the GridView control displays no data.

<%@ 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>

下列範例示範如何 QueryStringParameter 使用參數化 SQL 查詢,建立物件以顯示 Access 資料庫中的資料。The following example shows how to create a QueryStringParameter object to display data from an Access database by using a parameterized SQL query. AccessDataSource物件會抓取顯示在控制項中的記錄 GridViewThe AccessDataSource object retrieves records that are then displayed in a GridView control. GridView控制項也是可編輯的,可讓使用者在 Northwind 商貿 orders 資料表中更新訂單的狀態。The GridView control is also editable, and lets users update the status of orders in the Northwind Traders Orders table.

<%@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 要求查詢字串之一部分傳遞的欄位值,系結至參數,以用於參數化查詢或命令。You can use the QueryStringParameter class to bind the value of a field that is passed as part of an HTTP request query string to a parameter that is used in a parameterized query or command. 此欄位會從集合中抓取 QueryStringThe field is retrieved from the QueryString collection.

如果參考了物件,將資料系結至參數的控制項可能會擲回例外狀況 QueryStringParameter ,但不會傳遞對應的查詢字串名稱/值組。Controls that bind data to the parameter might throw an exception if a QueryStringParameter object is referenced, but no corresponding query-string name/value pair is passed. 同樣地,如果在沒有對應值的情況下傳遞查詢字串功能變數名稱,它們可能不會顯示任何資料。Similarly, they might display no data if the query-string field name is passed without a corresponding value. 若要避免這些情況,請 DefaultValue 在適當時設定屬性。To avoid these situations, set the DefaultValue property where appropriate.

QueryStringParameter類別會提供 QueryStringField 屬性,以識別要系結之查詢字串值的名稱。The QueryStringParameter class provides the QueryStringField property, which identifies the name of the query string value to bind to. 它也會提供繼承自類別的屬性 ParameterIt also provides the properties that are inherited from the Parameter class.

重要

QueryStringParameter類別不會驗證傳遞的值,它會提供原始值。The QueryStringParameter class does not validate the value that is passed; it provides the raw value. 不過,您可以 QueryStringParameter 在資料來源控制項中驗證物件的值。However, you can validate the value of a QueryStringParameter object in a data source control. 若要這麼做,請 Selecting 處理 Updating Inserting 資料來源控制項的、、或 Deleting 事件,並檢查事件處理常式中的參數值。To do so, handle the Selecting, Updating, Inserting, or Deleting event of the data source control and check the parameter value in the event handler. 如果參數的值未通過驗證測試,您可以將 Cancel 相關聯類別的屬性設定為,以取消資料作業 CancelEventArgs trueIf the value of the parameter does not pass the validation tests, you can cancel the data operation by setting the Cancel property of the associated CancelEventArgs class to true.

建構函式

QueryStringParameter()

初始化 QueryStringParameter 類別未命名的新執行個體。Initializes a new unnamed instance of the QueryStringParameter class.

QueryStringParameter(QueryStringParameter)

初始化 QueryStringParameter 類別的新執行個體,使用由 original 參數指定的執行個體值。Initializes a new instance of the QueryStringParameter class, using the values of the instance that is specified by the original parameter.

QueryStringParameter(String, DbType, String)

使用指定的查詢字串欄位和參數的資料型別,初始化 QueryStringParameter 類別的新具名執行個體。Initializes a new named instance of the QueryStringParameter class, using the specified query-string field and the data type of the parameter.

QueryStringParameter(String, String)

使用指定的字串識別要繫結的查詢字串欄位,初始化 QueryStringParameter 類別的新具名執行個體。Initializes a new named instance of the QueryStringParameter class, using the specified string to identify which query-string field to bind to.

QueryStringParameter(String, TypeCode, String)

使用指定的字串識別要繫結的查詢字串欄位,初始化 QueryStringParameter 類別的新具名強型別 (Strongly Typed) 執行個體。Initializes a new named and strongly typed instance of the QueryStringParameter class, using the specified string to identify which query-string field to bind to.

屬性

ConvertEmptyStringToNull

取得或設定值,該值指示是否應將 Parameter 物件繫結至的值轉換成 null (如果其為 Empty)。Gets or sets a value indicating whether the value that the Parameter object is bound to should be converted to null if it is Empty.

(繼承來源 Parameter)
DbType

取得或設定參數的資料庫型別。Gets or sets the database type of the parameter.

(繼承來源 Parameter)
DefaultValue

指定參數的預設值,當呼叫 Evaluate(HttpContext, Control) 方法時,要繫結的值應是此參數未初始化的值。Specifies a default value for the parameter, should the value that the parameter is bound to be uninitialized when the Evaluate(HttpContext, Control) method is called.

(繼承來源 Parameter)
Direction

表示此 Parameter 物件是否用來將值繫結至控制項,或是這個控制項是否可用來變更該值。Indicates whether the Parameter object is used to bind a value to a control, or the control can be used to change the value.

(繼承來源 Parameter)
IsTrackingViewState

取得值,指出 Parameter 物件是否正在將變更儲存到它的檢視狀態。Gets a value indicating whether the Parameter object is saving changes to its view state.

(繼承來源 Parameter)
Name

取得或設定參數的名稱。Gets or sets the name of the parameter.

(繼承來源 Parameter)
QueryStringField

取得或設定參數所繫結至的查詢字串欄位名稱。Gets or sets the name of the query-string field that the parameter binds to.

Size

取得或設定參數的大小。Gets or sets the size of the parameter.

(繼承來源 Parameter)
Type

取得或設定參數的類型。Gets or sets the type of the parameter.

(繼承來源 Parameter)
ValidateInput

取得或設定是否正在驗證查詢字串參數的值。Gets or sets whether the value of the query string parameter is being validated or not.

ViewState

取得狀態資訊的字典,允許您在相同頁面的多個要求之間,儲存和還原 Parameter 物件的檢視狀態。Gets a dictionary of state information that allows you to save and restore the view state of a Parameter object across multiple requests for the same page.

(繼承來源 Parameter)

方法

Clone()

傳回目前 QueryStringParameter 執行個體的複製。Returns a duplicate of the current QueryStringParameter instance.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

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

更新並傳回 QueryStringParameter 物件的值。Updates and returns the value of the QueryStringParameter object.

GetDatabaseType()

取得 DbType 值,該值等於目前 Parameter 執行個體的 CLR 型別。Gets the DbType value that is equivalent to the CLR type of the current Parameter instance.

(繼承來源 Parameter)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
LoadViewState(Object)

將資料來源檢視還原成之前所儲存的檢視狀態。Restores the data source view's previously saved view state.

(繼承來源 Parameter)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
OnParameterChanged()

呼叫 OnParametersChanged(EventArgs) 集合的 ParameterCollection 方法,該集合包含給定的 Parameter 物件。Calls the OnParametersChanged(EventArgs) method of the ParameterCollection collection that contains the Parameter object.

(繼承來源 Parameter)
SaveViewState()

儲存自頁面回傳至伺服器以來 Parameter 物件檢視狀態的變更。Saves the changes to the Parameter object's view state since the time the page was posted back to the server.

(繼承來源 Parameter)
SetDirty()

標記 Parameter 物件,以便將其狀態記錄在檢視狀態中。Marks the Parameter object so its state will be recorded in view state.

(繼承來源 Parameter)
ToString()

將這個執行個體的值轉換為它的相等字串表示。Converts the value of this instance to its equivalent string representation.

(繼承來源 Parameter)
TrackViewState()

會造成 Parameter 物件追蹤其檢視狀態變更,以將這些變更儲存在控制項的 ViewState 物件中,並持續存取相同頁面的其他要求。Causes the Parameter object to track changes to its view state so they can be stored in the control's ViewState object and persisted across requests for the same page.

(繼承來源 Parameter)

明確介面實作

ICloneable.Clone()

傳回目前 Parameter 執行個體的複製。Returns a duplicate of the current Parameter instance.

(繼承來源 Parameter)
IStateManager.IsTrackingViewState

取得值,指出 Parameter 物件是否正在將變更儲存到它的檢視狀態。Gets a value indicating whether the Parameter object is saving changes to its view state.

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

將資料來源檢視還原成之前所儲存的檢視狀態。Restores the data source view's previously saved view state.

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

儲存自頁面回傳至伺服器以來 Parameter 物件檢視狀態的變更。Saves the changes to the Parameter object's view state since the time the page was posted back to the server.

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

會造成 Parameter 物件追蹤其檢視狀態變更,以將這些變更儲存在控制項的 ViewState 物件中,並持續存取相同頁面的其他要求。Causes the Parameter object to track changes to its view state so they can be stored in the control's ViewState object and persisted across requests for the same page.

(繼承來源 Parameter)

適用於

另請參閱