在asp.net中使用javascript保持自动完成功能 [英] Stuck in autocomplete using javascript in asp.net

查看:80
本文介绍了在asp.net中使用javascript保持自动完成功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的javascript:



This is my javascript :

<script src="js/jquery.js" type="text/javascript"></script>
     <link href="css/default.css" rel="stylesheet" type="text/css"  media="screen" />
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
        <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
        <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
        <script language="javascript" type="text/javascript">
            $(function () {
                $('#<%=txtSearch.ClientID%>').autocomplete({
                    source: function (request, response) {
                        $.ajax({
                            url: "Members1.aspx/GetName",
                            data: "{ 'pre':'" + request.term + "'}",
                            dataType: "json",
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            success: function (data) {
                                response($.map(data.d, function (item) {
                                    return { value: item }
                                }))
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                alert(textStatus);
                            }
                        });
                    }
                });
            });
</script>





这是我的CS代码





this is my CS code

public partial class CP_News : System.Web.UI.Page
{
 public string msg2;
 static string search = "";
    SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString); 
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            if (Session["name"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                           
                //BindRepeater();
                allBindRepeater();
                
            }
        }
        search = DropDownListSearch.SelectedItem.Text;

    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static List<string> GetName(string pre)
    {


        List<string> allSearchName = new List<string>();



        string key = search;
        using (SqlConnection conn = new SqlConnection())
        {
            conn.ConnectionString = ConfigurationManager
                    .ConnectionStrings["ConnectionString2"].ConnectionString;



            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                conn.Open();



         if (key == "Mobile")
                {
                    cmd.CommandText = "SELECT ContactNo FROM PersonalInformation where " +
              "ContactNo like N'" + pre + "%'";
                    using (SqlDataReader sdr = cmd.ExecuteReader())
                    {
                        while (sdr.Read())
                        {
                            allSearchName.Add(string.Format("{0}", sdr["ContactNo"]));
                        }
                    }

                }




                conn.Close();
            }
            return allSearchName;
        }



    }







为文本框:

< asp:TextBox ID =txtSearchrunat =server>





使用断点测试后,我发现在文本框中输入时控件不会在Getname函数内移动。



请求我知道mi错在哪里!!




for the textbox :
<asp:TextBox ID="txtSearch" runat="server" >


After testing using breakpoint, I found that the control doesn't move inside the "Getname" Function while typing in the textbox.

Pleas let me know where m i wrong!!

推荐答案

function (){

' #<%= txtSearch.ClientID%>')。autocomplete( {
source: function (request,response){
('#<%=txtSearch.ClientID%>').autocomplete({ source: function (request, response) {


.ajax({
url:< span class =code-string> Members1.aspx / GetName
data: {'pre':' + request.term + '}
dataType: json
type: POST
contentType: 应用/ JSON; charset = utf-8
成功:功能(数据){
response(
.ajax({ url: "Members1.aspx/GetName", data: "{ 'pre':'" + request.term + "'}", dataType: "json", type: "POST", contentType: "application/json; charset=utf-8", success: function (data) { response(


这篇关于在asp.net中使用javascript保持自动完成功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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