如何在没有文本输入到依赖文本框中时使用gridview来显示所有表格行? [英] How to get a gridview to show all table rows when no text is entered in a dependent textbox?
问题描述
下面的工作正常,并基于在我的文本框中输入的文本过滤我的gridview。
当没有文本输入到我的文本框中时,我得不到结果并且无法理解为什么。
我的问题
如何让gridview显示所有表格行中没有文本输入到文本框中?
MSSQL
@Search nvarchar(50)
SELECT [table]。[column]
FROM [table]
WHERE [ table]。[column] LIKE'%'+ @Search +'%'OR COALESCE(@Search,'')=''
MARKUP
< asp:TextBox ID =txtSearch RunAt =ServerText =/>
< asp:SqlDataSource ID =sqlSearchRunAt =ServerSelectCommand =spSearchSelectCommandType =StoredProcedure>
< SelectParameters>
< asp:ControlParameter Type =StringName =SearchControlID =txtSearchPropertyName =Text/>
< / SelectParameters>
< / asp:SqlDataSource>
GRIDVIEW结果
我试过并测试了很多在这里找到的方法,但是当字符串为空时,所有返回0的结果都是。 在取消选择所有空参数 - 默认情况下,这防止 select参数的值为null时调用select命令。将它设置为 false
后,即使文本框为空,选择的SP仍然会被调用,例如,
< asp:SqlDataSource ID =sqlSearchRunAt =服务器
CancelSelectOnNullParameter =false.. >
< / asp:SqlDataSource>
SqlDataSource在这里似乎有点误导我,因为TextBox.Text在空时返回String.Empty ,不为null,因此我不希望为TextBox混淆CancelSelectOnNullParameter,但似乎我们必须。
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.
MY QUESTION
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,'') = ''
MARKUP
<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>
RESULTS TO GRIDVIEW
I have tried and tested many methods found on here however all return 0 results when string is empty.
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>
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屋!