问题描述
我想知道如何根据在文本框中输入的字符动态搜索或过滤网格视图中的记录.实现这一目标的最佳方法是什么?任何示例代码或示例都会非常有帮助.
i want to know as how to search or filter records in a gridview dynamically based on the character(s) entered on a textbox. What is the best way to achieve this? Any sample codes or examples will be really helpful.
推荐答案
这里的技巧是仅在搜索框上的文本发生变化时进行数据绑定,但您必须始终设置数据源选择命令代码.因此,您添加一个文本框和一个表示提交的按钮,然后您将拥有以下内容:
The trick here is to make databind only when the text change on the search box, but you must always set the datasource select command code. So you add a text box, and a button that say, submit, and you have the following:
OnPageLoad ->
if(SearchContron.Text.Length > 0)
SqlDataSource1.SelectCommand = "SELECT * FROM TABLE WHERE Desc LIKE N'%" + SearchContron.Text +"%'"
else
SqlDataSource1.SelectCommand = "SELECT * FROM TABLE "
和
OnSubmitButtonClick -> GridView.DataBind()
如果不这样做,分页和编辑等命令会失败.如果您从文本框中获取文本并将其分解为多个单词并在同一 sql 命令上单独搜索每个单词,您还可以使其更高级.
If you do it other way, the paging and editing and other commands will fail. You can also make it more advanced if you get the text from the text box and break it in many words and search each one as separate on the same sql command.
这篇关于使用asp.net中的文本框进行Gridview过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!