如何在一个文本框中搜索多个值 [英] How to search multiple values in one textbox

查看:144
本文介绍了如何在一个文本框中搜索多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我正在写一个例子,我可以在一个文本框中搜索多个名字。



目前,我使用以下代码:



Dear all,

I am writing to seek an example, where i can search multiple names in one textbox.

Currently, I am using the following code:

Search Name
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="Search" />
<hr />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat="server" AutoGenerateColumns="false" DataSourceID="GridDataSource" AllowPaging="true">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
        <asp:BoundField DataField="Item" HeaderText="Item" ItemStyle-Width="150" />
        <asp:BoundField DataField="desc" HeaderText="Description" ItemStyle-Width="150" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="GridDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"
    SelectCommand="SELECT Name, item, desc FROM deal" FilterExpression="Name LIKE '{0}%'">
    <FilterParameters>
        <asp:ControlParameter Name="Name" ControlID="txtSearch" PropertyName="Text" />
    </FilterParameters>
</asp:SqlDataSource>

推荐答案

ConnectionStrings:ConStr%>
SelectCommand = SELECT Name,item,desc FROM deal FilterExpression = 名称类似'{0}%' >
< FilterParameters>
< asp:ControlParameter Name = 名称 ControlID = txtSearch PropertyName = 文字 />
< / FilterParameters >
< / asp:SqlDataSource >
ConnectionStrings:ConStr %>" SelectCommand="SELECT Name, item, desc FROM deal" FilterExpression="Name LIKE '{0}%'"> <FilterParameters> <asp:ControlParameter Name="Name" ControlID="txtSearch" PropertyName="Text" /> </FilterParameters> </asp:SqlDataSource>


假设您想要一个包含两个或更多名称的文本框,并且您想在某个数据库中搜索一个或多个名称...

这不是那么简单。首先,你需要你的用户指出一个名字结束的位置:所以让他输入逗号或分号,例如:

Assuming you mean you want a textbox which contains two or more names and you want to search some database for one or more of the names...
It's not quite that simple. First, you need your user to indicate where a name ends: so get him to type commas or semicolons for example:
dave, pete, mike

然后假设您正在谈论SQL,您需要将其转换为SQL可以接受的格式:

Then assuming you are talking about SQL, you need to convert this to a format that SQL can accept:

string s = "dave, pete, mike";
string[] names = s.Split(',').Select(n => string.Format("'{0}'", n.Trim())).ToArray();
string sql = string.Format("SELECT * FROM MyTables WHERE username IN ({0})", string.Join(",", names));



但是......这非常危险 - 它让您对SQL注入攻击持开阔态度,因此您可能需要清理用户输入。


But...That is pretty dangerous - it leaves you wide open to SQL Injection attack, so you will probably have to sanitize your user input.


这篇关于如何在一个文本框中搜索多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆