ASP.NET Webform jQuery Select2远程数据访问

时间:2015-07-13 08:10:55

标签: c# asp.net web-services webforms jquery-select2

自从过去两天以来,我正在尝试从我的Web服务实现jQuery Select2远程数据访问的功能。我正在使用ASP.NET Web表单应用程序和Webservice,以下是我的WebForm和WebService,我没有从我的Web服务获取任何数据,请帮助

Default.aspx的

 
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="js/jquery-2.1.4.js"></script>
    <script src="js/select2.js"></script>
    <link href="css/select2.css" rel="stylesheet" />

    <script type="text/javascript">
        $(document).ready(function () {

            $('#ddStudents').select2({
                placeholder: "Search Student",
                minimumInputLength: 3,
                multiple: false,
                ajax: {
                    url: "StudentWebService.asmx/GetStudentSearch",
                    dataType: 'json',
                    type: "POST",
                    contentType: "application/json; charset-utf-8",
                    quietMillis: 100,
                    data: function (term, page) {
                        return {
                            q: term,
                            page_limit: 10
                        };
                    },
                    results: function (data, page) {
                        return { results: data };
                    }
                }
            });

        });
    </script>


</head>
<body>
    <form id="form1" runat="server">
        <div> 
            <asp:DropDownList ID="ddStudents" runat="server">
                <asp:ListItem Value="0">Select Student</asp:ListItem>
            </asp:DropDownList>
        </div>
    </form>
</body>
</html>

StudentWebService.asmx

  using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.Script.Serialization;
    using System.Web.Services;



public class Student
{
    public string StudentID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

}

    /// <summary>
    /// Summary description for StudentWebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class StudentWebService : System.Web.Services.WebService
    {
        private string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["JobPortalConnectionString"].ConnectionString;


        [WebMethod]
        public void GetStudentSearch(string term)
        {

            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.CommandText = "SelectStudentsSerach";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@FirstName", term));


                if (connection.State == ConnectionState.Closed)
                    connection.Open();
                DataSet dSet = new DataSet();
                SqlDataAdapter sqlAdapter;
                sqlAdapter = new SqlDataAdapter();

                if (connection.State == ConnectionState.Closed)
                    connection.Open();

                sqlAdapter.SelectCommand = command;
                sqlAdapter.Fill(dSet);

                if (connection.State == ConnectionState.Open)
                    connection.Close();


                List<Student> listStudent = new List<Student>();

                if (dSet.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow item in dSet.Tables[0].Rows)
                    {
                        Student s = new Student();
                        s.StudentID = item["StudentID"].ToString();
                        s.FirstName = item["FirstName"].ToString();
                        s.LastName = item["LastName"].ToString();

                        listStudent.Add(s);
                    }
                }

                JavaScriptSerializer js = new JavaScriptSerializer();

                Context.Response.Write(js.Serialize(listStudent));

            }
            catch (Exception)
            {

            }
            finally
            {
                connection.Close();
            }

        }

    }

另外我设置我的Web.Config文件添加了以下标记

 <webServices>
        <protocols>
          <add name="HttpGet"/>
          <add name="HttpPost"/>
        </protocols>
      </webServices>

0 个答案:

没有答案