当在相关文本框中没有输入文本时,如何让 gridview 显示所有表格行? [英] How to get a gridview to show all table rows when no text is entered in a dependent textbox?
问题描述
以下工作正常,并根据在我的文本框中输入的文本过滤我的 gridview.
The below works correctly and filters my gridview based on the text entered in my textbox.
当没有文本输入到我的文本框中时,我没有得到任何结果并且无法理解为什么.
When no text is entered into my textbox I get no results and cannot understand why.
我的问题
当文本框中没有输入文本时,如何让网格视图显示所有表格行?
How to get a gridview to show all table rows when no text is entered in the textbox?
MSSQL
@Search nvarchar(50)
SELECT [table].[column]
FROM [table]
WHERE [table].[column] LIKE '%' + @Search + '%' OR COALESCE(@Search,'') = ''
标记
<asp:TextBox ID="txtSearch" RunAt="Server" Text=""/>
<asp:SqlDataSource ID="sqlSearch" RunAt="Server" SelectCommand="spSearch" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Type="String" Name="Search" ControlID="txtSearch" PropertyName="Text"/>
</SelectParameters>
</asp:SqlDataSource>
GRIDVIEW 的结果
我已经尝试并测试了这里的许多方法,但是当字符串为空时,所有方法都返回 0 结果.
I have tried and tested many methods found on here however all return 0 results when string is empty.
推荐答案
查看 cancelselectonnull parameter - 默认情况下,此可防止选择参数值为null时调用select命令.将其设置为 false
后,即使文本框为空,选择 SP 仍将被调用,例如
Look at CancelSelectOnNullParameter - by default, this prevents the select command from being called when a select parameter's value is null. After setting it to false
, the select SP will then still get called even if the text box is empty, e.g.
<asp:SqlDataSource ID="sqlSearch" RunAt="Server"
CancelSelectOnNullParameter="false" ...>
</asp:SqlDataSource>
这里的 SqlDataSource 对我来说似乎有点误导,因为 TextBox.Text 在它为空时返回 String.Empty,而不是 null,因此我不希望必须为 TextBox 使用 CancelSelectOnNullParameter,但似乎我们必须.
The SqlDataSource seems a bit misleading to me here, because TextBox.Text returns String.Empty when it's empty, not null, therefore I wouldn't expect to have to mess with CancelSelectOnNullParameter for a TextBox, but it seems we have to.
这篇关于当在相关文本框中没有输入文本时,如何让 gridview 显示所有表格行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!