locked
how make 2 Textbox Autocomplete on the same page RRS feed

  • Question

  • User1865404792 posted

    hi all

    I want to make 2 autocomplete textbox In the same page

    do I type this code twice?

    Like this

    ///////////////////////////////// 1111111111111111111111111111111111
        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
    
        public static List<string> GetCompletionList(string prefixText)
        {
            using (OracleConnection con = new OracleConnection("data source=localhost:1521/orcl; user id=alhakimy; password=alhakimyyes;"))
            {
                using (OracleCommand com = new OracleCommand())
                {
                    com.CommandText = "select doc_no, doc_name from doctors where city_no= " + HttpContext.Current.Session["city_no"].ToString() + " and doc_name like '%" + prefixText + "%' ";
                    com.Parameters.Add("TextBox1", prefixText);
                    com.Connection = con;
                    con.Open();
                    List<string> summ = new List<string>();
                    using (OracleDataReader sdr = com.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            summ.Add(string.Format("{0}-{1}", sdr["DOC_NAME"], sdr["Doc_NO"]));//اذا اضفنا عمود جملة السكلته سنضيفه هنا كالبقية
                        }
                    }
                    con.Close();
                    return summ;
                }
            }
        }
    
        ////22222222222222222222222222222222222222222222222222222222222222222222222
        
        [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
    
        public static List<string> GetCompletionList2(string prefixText2)
        {
            using (OracleConnection con2 = new OracleConnection("data source=localhost:1521/orcl; user id=alhakimy; password=alhakimyyes;"))
            {
                using (OracleCommand com = new OracleCommand())
                {
                    com.CommandText = "select pharms_no, pharms_name from pharms where city_no= " + HttpContext.Current.Session["city_no"].ToString() + " and pharms_name like '%" + prefixText2 + "%' ";
                    com.Parameters.Add("TextBox3", prefixText2);
                    com.Connection = con2;
                    con2.Open();
                    List<string> summ2 = new List<string>();
                    using (OracleDataReader sdr2 = com.ExecuteReader())
                    {
                        while (sdr2.Read())
                        {
                            summ2.Add(string.Format("{0}-{1}", sdr2["pharms_NAME"], sdr2["pharms_NO"]));//اذا اضفنا عمود جملة السكلته سنضيفه هنا كالبقية
                        }
                    }
                    con2.Close();
                    return summ2;
                }
            }
        }
        //////////////////////////////////////////////

    Because the second coke did not work

    Monday, December 14, 2020 5:10 PM

All replies

  • User475983607 posted

    There is no need for two web methods that do the same thing. 

    You did not explain what is "not working".   The web method response is unusual for an autocomplete.  Most likely what's "not working" is a bug on the client which we cannot see.  Is there anyway you can share enough code to reproduce this issue?

    Monday, December 14, 2020 5:49 PM
  • User-1330468790 posted

    Hi alhakimy,

     

    The problem is related to the autocomplete textbox but you only shared backend codes.

    The codes looks like correct so we need to check how you write autocomplete textbox to fetch options from backend codes.

    Could you please also provide the textbox related codes/markups?

     

    Moreover, you could also set breakpoints on above codes to check whether the problem is :

    • the method is not triggered 
    • Or, the method is triggered but get errors

     

    Best regards,

    Sean

    Tuesday, December 15, 2020 2:17 AM
  • User1535942433 posted

    Hi alhakimy,

    Accroding to your description,I have created a test with two textbox autocomplete and it works fine.

    Follow is my page:

    <script type="text/javascript">  
            $(document).ready(function () {  
                $("#txtCountry").autocomplete({  
                    source: function (request, responce) {  
                        $.ajax({  
                            url: "2171631.aspx/GetCountryNames",  
                            method: "post",  
                            contentType: "application/json;charset=utf-8",  
                            data: JSON.stringify({ term: request.term }),  
                            dataType: 'json',  
                            success: function (data) {  
                                responce(data.d);  
                            },  
                            error: function (err) {  
                                alert(err);  
                            }  
                        });  
                    }  
                });
                 $("#txtnum").autocomplete({  
                    source: function (request, responce) {  
                        $.ajax({  
                            url: "2171631.aspx/Getnum",  
                            method: "post",  
                            contentType: "application/json;charset=utf-8",  
                            data: JSON.stringify({ term: request.term }),  
                            dataType: 'json',  
                            success: function (data) {  
                                responce(data.d);  
                            },  
                            error: function (err) {  
                                alert(err);  
                            }  
                        });  
                    }  
                });  
            });  
        </script>  
      <asp:TextBox ID="txtCountry" CssClass="form-control col-md-3" runat="server"></asp:TextBox>
      <asp:TextBox ID="txtnum" CssClass="form-control col-md-3" runat="server"></asp:TextBox> 

    Could you post your full codes about textboxes and jquery to us?It will help us to solve your problems.

    By the way,I suggest you need to check the webmethod's name of the second autocomplete ajax.

    Best regards,

    Yijing Sun

    Tuesday, December 15, 2020 2:36 AM
  • User1865404792 posted
    I used thes code for Autocomplete Textbox 1
    And it works Well
    [System.Web.Script.Services.ScriptMethod()]
        [System.Web.Services.WebMethod]
    
        public static List<string> GetCompletionList(string prefixText)
        {
            using (OracleConnection con = new OracleConnection("data source=localhost:1521/orcl; user id=alhakimy; password=alhakimyyes;"))
            {
                using (OracleCommand com = new OracleCommand())
                {
                    com.CommandText = "select doc_no, doc_name from doctors where city_no= " + HttpContext.Current.Session["city_no"].ToString() + " and doc_name like '%" + 
    
    prefixText + "%' ";
                    com.Parameters.Add("TextBox1", prefixText);
                    com.Connection = con;
                    con.Open();
                    List<string> summ = new List<string>();
                    using (OracleDataReader sdr = com.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            summ.Add(string.Format("{0}-{1}", sdr["DOC_NAME"], sdr["Doc_NO"]));//اذا اضفنا عمود جملة السكلته سنضيفه هنا كالبقية
                        }
                    }
                    con.Close();
                    return summ;
                }
            }
        }
    ///////////////////////////////////////////////////////
    <script type="text/javascript">
        $(function () {
            $("[id$=TextBox1]").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: '<%=ResolveUrl("~/daily.aspx/GetCompletionList") %>',
                        data: "{ 'prefixText': '" + 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$=hfdoc_no]").val(i.item.val);
                },
                ///////////هذا القسم يقو باضهار رساله في حاله قام المستخدم بكتابة اسم من خارج القائمة
                change:function(e, ui){
                    if(!(ui.item)){
                        e.target.value="";
                        alert("الاختيار من القائمة فقط");
                    }
                },
                ///////////
                minLength: 1
            });
        });  
    </script>
    ///////////////////////////////////////////////////////////////////////////////////////
    <asp:TextBox ID="TextBox1" runat="server" onpaste="return false" oncopy="return false" oncut="return false" Height="30px" style="font-size: large; font-weight: 700" Width="235px"  
    Enabled="False" autocomplete="off"></asp:TextBox>
      <cc1:AutoCompleteExtender ID="TextBox1_AutoCompleteExtender" runat="server" TargetControlID="TextBox1" ServiceMethod="GetCompletionList" 
    Enabled="False" CompletionInterval="500"> </cc1:AutoCompleteExtender>

    Now I want to create an Autocomplete Textbox 2 for another select sentence 

    com.CommandText = "select pharms_no, pharms_name from pharms where city_no= " + HttpContext.Current.Session["city_no"].ToString() + " and pharms_name like '%" + prefixText2 + "%' ";

    Tuesday, December 15, 2020 6:51 AM
  • User1535942433 posted

    Hi alhakimy,

    Please post your textbox2 code.You couldn't find problems of these codes you have worked.

    Best regards,

    Yijing Sun

    Tuesday, December 15, 2020 8:49 AM