简体   繁体   中英

Handling Optional Query Parameters

I have a GridView bound to an DataSource query with parameters tied to form controls.

What is the standard / best-practices way to handle optional query parameters?

Something like.. set the control parameter to -1 then in your query do something like...

Select * From Blah Where
(Somefield = @param or @param = -1)

On your DataSource, have your ControlParam use the DefaultValue:

<asp:ControlParameter Name="CustomerID" 
    ControlID="DropDownList1" PropertyName="SelectedValue"  
    DefaultValue="-1" />

Then in your SQL statement, check for that same default value from your ControlParameter:

 SELECT * 
 FROM Invoices
 WHERE (CustomerID = @CustomerID OR @CustomerID = -1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM