使用Jquery Ajax引入部分数据

时间:2012-11-08 15:14:16

标签: asp.net jquery

我是Jquery的新手。我在aspx页面上有一个文本框。当输入2个字母时,文本框使用jquery ajax从sql server数据库中带来土耳其邮政编码。但是如果从sql server返回的记录超过900条,则jquery会给出“内部服务器错误”。我认为将数据带到文本框需要更多时间,但jquery不允许这样做并给出内部错误。现在我希望在第一次请求时提供部分数据。例如;如果用户在文本框中写入34,则ajax将不会带来以34开头的所有数据。它将带来前100个结果。然后,当用户将滚动条滑动到记录的末尾时,将查询新记录并向用户显示。就像facebook posts一样。这可能吗?如果是的话,如果您与我们分享示例代码,我将很高兴。 这是我的代码..

的Javascript

$(function() {
$(".tb").autocomplete
        ({
             source: function(request, response) {
                $.ajax({
                    type: "POST",
                    url: "WebService_GetData.asmx/GetCmbPostaKod",
                    dataType: "json",
                    //data: "{ 'filterKey': '" + request.term + "','select1': '" + $('#cmbLogisticType').val() + "' }",
                    data: JSON.stringify({filterKey: request.term, lojtype:$('#cmbLogisticType').val(), ulke:$asp('ulke').val()}),
                    contentType: "application/json; charset=utf-8",
                    dataFilter: function(data) { return data; },
                    success: function(data) {
                        response($.map(data.d, function(item) {
                        return { value: item.PostaKodu, label: item.SemtAdi + ' / ' + item.IlceAdi + ' / ' + item.IlAdi + ' - ' + item.PostaKodu }
                        }))
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(errorThrown);
                    }
                });
            },
            minLength: 2,
            multiple: true,
            matchContains: true,
            formatItem: formatItem,
            formatResult: formatResult
        });
    });

aspx.cs背后的代码

    [WebMethod]
    public List<PostaKod> GetCmbPostaKod(string filterKey, string lojtype, string ulke)
    {

        string stmt;
        DataTable dt;
        //myToolKit.AddValueToCombo(cmbVergiDairesi, "0", "Seçiniz");
        //myToolKit.AddValueToCombo(cmbSatisElemani, "0", "Seçiniz");
        FunctionsToolkit.InjectionKontrolEt(ref filterKey);
        mySqlKit.DoConnect();
        /*
        stmt = string.Format(@"SELECT ILADI, ILCEADI, SEMTADI, POSTAKODU, ULKEKODU FROM TBLPOSTAKODLARI (NOLOCK)
                               WHERE POSTAKODU LIKE '{0}%' AND ULKEKODU='{1}' ORDER BY POSTAKODU", filterKey, (lojtype=="3")?ulke:lojtype);
        */
        stmt = string.Format(@"EXEC sp_get_postakodu '{0}','{1}'",filterKey,(lojtype=="3")?ulke:lojtype);
        dt = mySqlKit.DoDTstr(stmt);
        mySqlKit.DoDisconnect();
        return mySqlKit.ComboDList(dt);
    }

页面aspx

    <div id="panel1">
       Nakliye Tipini Seçiniz* :
       <select name="LogisticType" id="cmbLogisticType" onchange="toggle(this.value)">
           <option value="1" selected="selected"> Seçiniz </option>
           <option value="90"> Şehiriçi </option>
           <option value="90"> Şehirlerarası </option>
           <option value="3"> Uluslararası </option>
        </select>
        <!--
       <asp:DropDownList ID="cmbPostaKodu" name="cmbPostaKodu" runat="server" class="combo">
       </asp:DropDownList>
       <select ID="cmbIl" name="cmbIl" runat="server" onchange="ilceGetir(this.options[this.options.selectedIndex].value)" class="combo"></select>
       <select ID="cmbIlce" name="cmbIlce" runat="server" class="combo"></select>-->
    </div>
    <div class="slidingDiv">
        <div class="slidingDiv2">
              Hareket Noktası (Ülke):
                <div class="ui-widget">
                    <div class="text1">
                        <asp:TextBox ID="ulke" class="tb" runat="server" onfocus="this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;">Ülke Seçiniz</asp:TextBox>
                    </div>
                </div>

                 Varış Noktası (Ülke):
                <div class="ui-widget">
                    <div class="text1">
                        <asp:TextBox ID="ulke2" class="tb" runat="server" onfocus="this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;">Ülke Seçiniz</asp:TextBox>
                    </div>
                </div>
        </div>
        Hareket Noktası (Posta Kodu):
        <div class="ui-widget">
            <div class="text1">
                <asp:TextBox ID="city" class="tb" runat="server" onfocus="this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;">Posta Kodu Giriniz</asp:TextBox>
            </div>
        </div>
        Varış Noktası (Posta Kodu): 
          <div class="ui-widget">

            <div class="text1">  
                <asp:TextBox ID="city2" class="tb" runat="server" onfocus="this.value = ( this.value == this.defaultValue ) ? '' : this.value;return true;">Posta Kodu Giriniz</asp:TextBox>
            </div>
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

最简单的方法是使用TOP n限制服务器端查询结果集(假设您使用的是SQL Server)。让n成为对你有意义的数字。没有用户会浏览900个选项。比方说,20。

如果用户想要更多选项,则需要更多输入。