自动完成TextBox不使用asp.net中的数据库值

时间:2015-11-19 04:49:36

标签: c# asp.net

我有一个文本框和一个数据库。每次用户在文本框中键入内容时,它都无法使用数据库。 这是我的default.aspx.cs代码文件,我希望得到您的帮助,提前感谢:)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;

namespace dotnetawsome
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScripMethod(ResponseFormat = ResponseFormat.Json)]
       public static List<TopCompany> GetCompanyName(String pre)
        {
            List<TopCompany> allCompany = new List<TopCompany>();
            using (MyDatabaseEntities dc = new MyDatabaseEntities())
            {
                allCompany = (from a in dc.TopCompanies
                              where a.CompanyName.StartWith(pre)
                              select a).ToList();
            }
            return allCompany;
        }
    }
}

这是我的default.aspx文件

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="dotnettest._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" src="script.js"></script>
<script src="jquery\jquery.js"></script>
<link rel="stylesheet" href="css/jquery-ui.css" />

    <script src="js/jquery-1.8.3.js" type="text/javascript" language="javascript"></script>

    <script src="js/jquery-ui.js" type="text/javascript" language="javascript"></script>

    <script type="text/javascript" language="javascript">

<style>
.ui-autocomplete
{
    font-size: 11px;
    }
</style>
 <script language="javascript" type="text/javascript">

    $(function (){
    $('#<%=textCompanyName.ClientID %>').autocomplete({
    source: function(request,response){
    $.ajax ({
    url: "default/GetCompanyName",
    data: "{'pre' :'" + request.term + "'}",
    dataType: "json",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    success: function(data){
    response($.map(data.d,function(item){
    return{
    CompanyName: item.CompanyName,
    Industry: item.Industry,
    json: item
    }

    }))
    },
    error: function (XMLHttpRequest, textStatus, errorThrown){
    alert(textStatus);
    }
    });
    },
    focus: function (event, ui){
    $('#<%=textCompanyName.ClientID %>').val(ui.item.CompanyName);
    return false;
    },
    select: function(event ui){
    $('#<%=textCompanyName.ClientID %>').val(ul.item.CompanyName);
   return false;
   },
   }).data("ui-autocomplete")._renderItem= function(ul, item){
   return $("<li>")
   .append("<a>Company:" +item.CompanyName +"<br> Industry: " + item.Industry + "</a>")
   .appendTo(ul)
   };
   });

</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
    <table>
    <tr>
    <td>  Card Name </td>
    <td>
    <div class="ui-widget" style="text-align:left">


        <asp:TextBox ID="textCompanyName" runat="server" CssClass="textboxAuto" Width="350px" Font-Size="12px"></asp:TextBox>

    </div>
    </td>


    </tr>

    </table>
</asp:Content>

0 个答案:

没有答案