Asp.net

asp:QueryStringParameter 和空查詢字元串參數

  • January 25, 2017

我已經asp:GridView使用asp:SqlDataSource. 我想限制客戶端顯示的資訊:

View.aspx必須顯示所有內容,View.aspx?client=1必須僅顯示來自客戶端 ID #1 的請求。

所以我使用<asp:QueryStringParameter Name="client" QueryStringField="client" />for query "EXEC getRequests @client"

指定某些客戶端時,一切正常。但不要——如果不是。

我使用 SSMS 測試了我的 SP - 它在兩種情況下都能正常工作 - 指定參數時和未指定參數時(NULL顯式傳遞)。

我做了什麼?

如果 SqlDataSource 的任何參數為空,則不會觸發 SqlDataSource,除非您另外指定:

<asp:SqlDataSource CancelSelectOnNullParameter="False" />

可能還需要為您的查詢字元串參數添加一個空預設值:

<asp:QueryStringParameter Name="client" QueryStringField="client" DefaultValue="" ConvertEmptyStringToNull="True" />

對於這些情況,您需要為參數定義一個預設值,例如:

<asp:QueryStringParameter Name="client" QueryStringField="client" DefaultValue="0"/>

然後在SP中你需要驗證客戶端是否為0,返回所有客戶端,否則返回特定客戶端。

引用自:https://stackoverflow.com/questions/2687048