简体   繁体   English

Adding two values to ajax autocomplete function in Asp.net web forms

[英]Adding two values to ajax autocomplete function in Asp.net web forms

I have search textbox and i whould like to implement autocomplete checking in it.我有搜索文本框,我想在其中实现自动完成检查。 When you type one letter autocomplete need to give you all the names with that letter.当您键入一个字母时,自动完成需要为您提供该字母的所有名称。 I found tutorial with that but my problem is what I whould like implement two radio buttons and if first one is checked it will give you only suggestion for Clients, and if second one is checked it will give you only suggestion for companies.我找到了教程,但我的问题是我想要实现两个单选按钮,如果选中第一个,它将只为您提供客户建议,如果选中第二个,它将只为您提供公司建议。

I thried with this code in Web service.asmx我在 Web service.asmx 中尝试使用此代码

[WebMethod]
public class Autocomplete : System.Web.Services.WebService
{
    [WebMethod]

    public List<string> GetClientNames(string searchTerm,string rbtnValue)
    {
        List<string> clientNames = new List<string>();
        string connStr = ConfigurationManager.ConnectionStrings["RheosConnString"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            if (rbtnValue == "radio0")
            {
                SqlCommand cmd = new SqlCommand("spGetClientNames", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter param = new SqlParameter("@term", searchTerm);
                cmd.Parameters.Add(param);
                conn.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {
                        clientNames.Add(sdr["ime"].ToString());

                }
            }
            else
            {
                SqlCommand cmd = new SqlCommand("spGetCompanyNames", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter param = new SqlParameter("@term", searchTerm);
                cmd.Parameters.Add(param);
                conn.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                while (sdr.Read())
                {

                        clientNames.Add(sdr["naziv"].ToString());

                }
            }

        }
        return clientNames;
    }

}

My Javascript looks like this我的 Javascript 看起来像这样

<script type="text/javascript" language="javascript">
        $(function () {
            $('#<%=txtSearch.ClientID%>').autocomplete({
                source: function (request, response) {
                    var value = $("form input[type='radio']:checked").val();
                    $.ajax({
                        url: "Autocomplete.asmx/GetStudentNames",
                        data: {searchTerm: request.term, rbtnValue:value},
                        type: "POST",
                        dataType: "json",
                        contentType: "application/json;charset=utf-8",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert('Postoji problem s dohvaćanjem zahtjeva');
                        }
                    });
                },
                minLength: 0
            });
        });

    </script>

Do I need implement something else for radiobuttons.我需要为单选按钮实现其他东西吗? My value is radio0 and radio1.我的值是radio0 和radio1。 Thank you!谢谢!

In you service the method name is GetClientNames but in your ajax call you are calling to method GetStudentNames .在您的服务中,方法名称是GetClientNames但在您的 ajax 调用中,您正在调用方法GetStudentNames Most probably this could be the issue.很可能这可能是问题所在。 If this is not the issue kindly share your error so I would be able to help you out.如果这不是问题,请分享您的错误,以便我能够帮助您。

Thanks for suggestion I change it.谢谢建议我改了。 But still not geting autocomplete My head code using master page但仍然没有自动完成我的头代码使用母版页

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <title>Klijenti</title>
    <script src="plugins/jquery-ui-1.12.1.custom/jquery-ui.js" type="text/javascript"></script>
    <script src="plugins/jquery-ui-1.12.1.custom/jquery-ui.min.js" type="text/javascript"></script>
    <link href="plugins/jquery-ui-1.12.1.custom/jquery-ui.min.css" rel="stylesheet" type="text/css" />
    <link href="plugins/jquery-ui-1.12.1.custom/jquery-ui.structure.min.css" rel="stylesheet" type="text/css" />
    <link href="plugins/jquery-ui-1.12.1.custom/jquery-ui.theme.min.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" language="javascript">
        $(function () {
            $('#<%=txtSearch.ClientID%>').autocomplete({
                source: function (request, response) {
                    var value = $("form input[type='radio']:checked").val();
                    $.ajax({
                        url: "Autocomplete.asmx/GetClientNames",
                        data: {searchTerm: request.term, rbtnValue:value},
                        type: "POST",
                        dataType: "json",
                        contentType: "application/json;charset=utf-8",
                        success: function (data) {
                            response(data.d);
                        },
                        error: function (result) {
                            alert('Postoji problem s dohvaćanjem zahtjeva');
                        }
                    });
                },
                minLength: 0
            });
        });

    </script>
</asp:Content>

And form components if this can help.如果这有帮助,还可以形成组件。 I dont know it's not showing any error and when I write Client name it list it but when put company name it doesnt list it.我不知道它没有显示任何错误,当我写客户名称时它会列出它,但是当输入公司名称时它不会列出它。 Its the same what ever radiobutton I check.它与我检查的单选按钮相同。

<div class="input-group">
                            <span class="input-group-addon">
                                <i class="fa fa-search"></i>
                            </span>
                            <asp:TextBox ID="txtSearch" runat="server" CssClass="form-control" placeholder="Unesi pojam..."></asp:TextBox>
                            <!-- Radio buttons-->
                            <div class="col-sm-6">
                                <label>
                                    <input type="radio" name="radio" value="radio0" />Klijenti
                                </label>
                            </div>
                            <div class="col-sm-6">
                                <label>
                                    <input type="radio" name="radio" value="radio1" />Tvrtke
                                </label>
                            </div>
                            <!-- End of radio buttons-->
                            <asp:Button ID="btnSearch" runat="server" class="btn btn-primary btn-block" Text="Pretraži" OnClick="btnSearch_Click"/>
                        </div>

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

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