System.Web.UI.WebControls Namespace
.NET Framework version 2.0
Binds the value of an HTTP request query string field to a parameter object.
You can use the QueryStringParameter class to bind the value of a field passed on an HTTP request query string to a parameter used in a parameterized query or command. The field is retrieved from the QueryString collection. Controls that bind data to the parameter might throw an exception if a QueryStringParameter is specified, but no corresponding query string name/value pair is passed. Similarly, they might display no data if the query string field name is passed with no corresponding value. Set the DefaultValue to avoid these situations where appropriate.
The QueryStringParameter class provides the QueryStringField property, which identifies the name of the query string value to bind to, in addition to those inherited from the Parameter class.
IMPORTANT: The QueryStringParameter does not validate the value passed by the query string in any way; it uses the raw value. In most cases you can validate the value of the QueryStringParameter before it is used by a data source control by handling an event, such as the Selecting, Updating, Inserting, or Deleting event exposed by the data source control you are using. If the value of the parameter does not pass your validation tests, you can cancel the data operation by setting the Cancel property of the associated CancelEventArgs class to true.
The below code demonstrates how to declaratively use a QueryStringParameter when displaying data in a FormView control. You declare and add the QueryStringParameter to the AccessDataSource control's SelectParameters collection, and it binds the value of the given query string field to the parameter placeholder ( PlanId = ?
) of the SelectCommand.
<asp:accessdatasource id = "planDetails" runat = "server"
datafile = "~/app_data/plans.mdb"
selectcommand = "SELECT * FROM Plans WHERE PlanId = ?"
datasourcemode = "datareader">
<selectparameters>
<asp:querystringparameter querystringfield = "id" />
</selectparameters>
</asp:accessdatasource>
<asp:formview id = "fvPlanDetails" runat = "server"
datasourceid = "planDetails"
width=95% enableviewstate=false>
<itemtemplate>
... template definition here ...
</itemtemplate>
</asp:formview>
The following examples show this code in action.
Using Parameters with Data Source Controls