简体   繁体   English

如何通过数据库在组合框中搜索

[英]how to search in combobox by database

protected void Page_Load(object sender, EventArgs e)
{
    dr = new MySqlDataAdapter("select * from cus_mas_det", conn);
    ds = new DataSet();

    dr.Fill(ds, "cus_mas_det");
    DataTable CodeDesc = new DataTable();
    conn.Open();
    dr = new MySqlDataAdapter("select tvl_code, concat_ws(',', tvl_code, citi_name) citiname from code_desc where travel_mode = 'BUS'", conn);
    ds1 = new DataSet();
    dr.Fill(ds1);
    ddlfrom.DataSource = ds1;
    ddlfrom.DataTextField = "citiname";
    ddlfrom.DataValueField = "tvl_code";
    ddlfrom.DataBind();
    txtbookingdate.Text = System.DateTime.Now.ToString("yyyy/MM/dd");
    txtbookingdate.ReadOnly = true;
    txtbookingref.Text = autoid();
    txtbookingref.ReadOnly = true;
}

I have a asp page in which I have textboxes and comboboxes, I am populating the combobox from the database, as given in the above code, and I am able to populate that. 我有一个ASP页面,其中有文本框和组合框,我从数据库中填充组合框,如上面的代码所示,并且能够填充该组合框。
Now my problem is I want to search the elemnets by typing any words in the combobox.. 现在我的问题是我想通过在组合框中键入任何单词来搜索elemnet。
For example, if I type word "Ab" it should show all the elements starting with "Ab".. 例如,如果我输入单词“ Ab”,则应显示所有以“ Ab”开头的元素。
How to do that? 怎么做?

   <asp:ComboBox ID="ddlfrom" class="chzn-select" runat="server"  
                                            DataTextField="name" DataValueField="name" MaxLength="0" 
                                            style="display: inline;" 
                                            onselectedindexchanged="ddlfrom_SelectedIndexChanged1">

                                        </asp:ComboBox>                                       

You need to do this client side using AJAX. 您需要使用AJAX进行此客户端。

  1. Create a web service or use a page method which takes as a parameter some search text and then returns the data you need. 创建一个Web服务或使用一个页面方法,该方法将一些搜索文本作为参数,然后返回所需的数据。

  2. Client side - handle the keypress event using JavaScript/jQuery and update your combo box or whatever control you are using at that point. 客户端-使用JavaScript / jQuery处理keypress事件,并更新您的组合框或此时使用的任何控件。

Example using jQuery: 使用jQuery的示例:

$('#<%= txtBox.ClientID %>').keypress(function () {

    var currentText = $(this).val();

    $.get('some_web_service', { searchText: currentText }, function (data) {
        // update combo box here using data
    });

});

This assumes you have a web service called some_web_service which will return the data you need. 假设您有一个名为some_web_service的Web服务,它将返回所需的数据。

Alternatively you could put the whole thing in an UpdatePanel and handle the keypress event but the performance of this will not be as good. 或者,您可以将整个事情放在UpdatePanel中并处理keypress事件,但是这样做的性能将不尽人意。

我不认为您可以使用ASP.Net组合框进行这种搜索,而应该尝试一些类似的操作

You can use chosen jquery plugin from here harvesthq.github.io/chosen/ 您可以在此处使用选定的jquery插件Harvesthq.github.io/chosen/

So you can search keyword within drop-down list try this plugin. 因此,您可以在下拉列表中搜索关键字,尝试使用此插件。

This may help you to use How do I use Chosen jQuery in my html file? 这可以帮助您使用如何在HTML文件中使用选择的jQuery?

Add required js and css files to page assign class to dropdown like this:- 将所需的js和css文件添加到页面,将类​​分配给下拉列表,如下所示:-

    <head>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css">
    <script src="http://code.jquery.com/jquery-1.8.3.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <script src="http://harvesthq.github.com/chosen/chosen/chosen.jquery.js"></script>
    <link rel="stylesheet" href="http://harvesthq.github.com/chosen/chosen/chosen.css">
<script type="text/javascript">
$(function(){
    $(".chzn-select").chosen();
});
</script>
    </head>
    <body>

Assign class to your dropdown 将课程分配给您的下拉菜单

<asp:DropDownList ID="dr_list" runat="server" CssClass="chzn-select">
   <asp:ListItem Value="-1" Text=""></asp:ListItem>
      </asp:DropDownList>

Download Here 在这里下载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM