繁体   English   中英

C#从SQL自动完成到文本框

[英]C# autocomplete to textbox from sql

今天是个好日子。

我的问题是如何根据在另一个文本框中输入的特定数据自动填充另一个文本框。 为了进一步说明,我的第一个文本框是从名称为“ code”的sql表自动完成的。 在该列旁边是一个称为“描述”的字段。基于从代码列填充的数据,我如何根据在第一个文本框中选择的值自动填充第二个文本框? 我真的希望有道理。

这是我正在更新textbox1的代码,它工作正常:

 private void liguaneaRxToolStripMenuItem_Click(object sender, EventArgs e) 
    {
        this.liguanea_LaneTableAdapter1.Fill(this.pharmaciesDataSet1.Liguanea_Lane);
        try
        {

            string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
            SqlConnection con = new SqlConnection(connectionString);
            con.Open();
            string query = "SELECT Code FROM dbo.Liguanea_Lane";
            SqlCommand cmd = new SqlCommand(query, con);

            SqlDataReader dr = cmd.ExecuteReader();
            AutoCompleteStringCollection mycollection = new AutoCompleteStringCollection();
            while (dr.Read())
            {

                mycollection.Add(dr.GetString(0));

             textBox1.AutoCompleteCustomSource = mycollection;
            con.Close();
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());
        }
    }

现在这是textbox2的代码,我想根据从文本框1中选择的值填写一个值。 在查询中,我将写为:“从dbo.Liguanea_Lane中选择描述,其中代码=“任何值。”其中“任何值”是从textbox1收集的输入,然后返回(填充)textbox2中附加的描述

 private void displayValIntoTextbox(string val ) //this function will auto complete the other textbox 
    {

        if (val == null) //this will check if the value is null 
        {
            MessageBox.Show("please enter a the correct code");
        }
        else
        {
            try
            {
                string connectionString = "Data Source=LPMSW09000012JD\\SQLEXPRESS;Initial Catalog=Pharmacies;Integrated Security=True";
                SqlConnection con = new SqlConnection(connectionString);
                con.Open();
                string query = "SELECT description FROM dbo.Liguanea_Lane where code= '"+val +"'"; // this query
                SqlCommand cmd = new SqlCommand(query, con);

                SqlDataReader dr = cmd.ExecuteReader();
                AutoCompleteStringCollection mycollection = new AutoCompleteStringCollection();
                if (dr.HasRows) // check if any info exist in database base off the query
                {
                    while (dr.Read())
                    {

                        mycollection.Add(dr.GetString(0));

                    }
                    textBox1.AutoCompleteCustomSource = mycollection;
                }
                else
                {
                    MessageBox.Show(val);
                }

                con.Close();
            }
            catch (SqlException sql)
            {
                MessageBox.Show(sql.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }

我希望每个人都跟随。 哦,它也是Winform应用程序

尝试这个-

对于从数据库自动完成文本框,请创建一个ID为'txtAutoComplete'的文本框控件

<asp:TextBox ID="txtSearch" runat="server" />
<asp:HiddenField ID="hfCustomerId" runat="server" />

然后为ajax添加jquery插件

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js" type="text/javascript"></script>

<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js" type="text/javascript"></script>

添加此ajax脚本以自动完成

 $(function () {
    $("[id$=txtSearch]").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '<%=ResolveUrl("~/Default.aspx/GetCustomers") %>',
                data: "{ 'prefix': '" + request.term + "'}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    response($.map(data.d, function (item) {
                        return {
                            label: item.split('-')[0],
                            val: item.split('-')[1]
                        }
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        select: function (e, i) {
            $("[id$=hfCustomerId]").val(i.item.val);
        },
        minLength: 1
    });
});  

在Default.aspx / GetCustomers方法中,编写C#代码以从数据库中获取名称列表。

对于CSS添加此样式表参考

<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/blitzer/jquery-ui.css"
rel="Stylesheet" type="text/css" />

暂无
暂无

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

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