c# - Jquery 自动完成扩展程序在回发后无法正常工作

标签 c# asp.net jquery

我在 ASP.Net 中使用 Web 服务使用 jQuery 自动完成功能。我使用自动完成功能来过滤员工代码。当页面加载时自动完成功能正常,但是当我单击搜索按钮后自动完成功能无法正常工作。

我认为问题在于 document.ready 函数,所以当页面加载时它工作正常,但我也必须在按钮单击事件后使用自动完成。 我该怎么做???

这是我的 jQuery 自动完成

<link href="../AutoComplete/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="../AutoComplete/jquery.min.js" type="text/javascript"></script>
<script src="../AutoComplete/jquery-ui.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
    $("#<%=txtEmpcode.ClientID %>").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '<%=ResolveUrl("~/MyWebService.asmx/FetchEmpCode") %>',
                data: "{ 'Empcode': '" + 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('-')[1],
                            //val: item
                        }
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        minLength: 1
    });
});
</script>

标记

<td align="right">
<asp:Label ID="lblEmpCodeSrch" runat="server" Text="EmpCode" CssClass="label">   </asp:Label>
 </td>
 <td>
  <asp:TextBox ID="txtEmpCodeSrch" runat="server" Width="250px" ToolTip="Enter Employeecode"></asp:TextBox>
   &nbsp;&nbsp;<asp:Button ID="bttnSearch" runat="server" CssClass="submit" Height="23px" Text="Search" onclick="bttnSearch_Click" />
  </td>

ButtonSearch 代码隐藏

protected void bttnSearch_Click(object sender, EventArgs e)
{
    clsEmp.EMPLOYEEID =txtEmpCodeSrch.Text.Trim()==""?                                                                                   0:Convert.ToInt32(hFieldEmpId.Value);
    DataTable dtEmp = clsEmp.GetDetails();
    if (dtEmp.Rows.Count > 0)
    {
        hFieldEmpId.Value = "";
       // txtEmpCodeSrch.Text = "";
        if (ViewState["Sort"] != null)
        {
            DataView dView = new DataView(dtEmp);
            dView.Sort = ViewState["Sort"].ToString();
            gdView.DataSource = dView;
            gdView.DataBind();
        }
        else
        {
            gdView.DataSource = dtEmp;
            gdView.DataBind();
        }
    }
}

最佳答案

当你有一个UpdatePanel,数据更新后,你还需要重新初始化javascript,因为旧的已经不能用了,因为你的html页面的struct变了,dom变了。

UpdatePanel 提供了一些在客户端执行此操作的功能,您的代码如下:

<script type="text/javascript"> 
   // if you use jQuery, you can load them when dom is read.
   $(document).ready(function () {
       var prm = Sys.WebForms.PageRequestManager.getInstance();    
       prm.add_initializeRequest(InitializeRequest);
       prm.add_endRequest(EndRequest);

       // Place here the first init of the autocomplete
       InitAutoCompl();
    });        

    function InitializeRequest(sender, args) {
    }

    function EndRequest(sender, args) {
       // after update occur on UpdatePanel re-init the Autocomplete
       InitAutoCompl();
    }

   function InitAutoCompl() {
      $("#<%=txtEmpcode.ClientID %>").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: '<%=ResolveUrl("~/MyWebService.asmx/FetchEmpCode") %>',
                data: "{ 'Empcode': '" + 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('-')[1],
                            //val: item
                        }
                    }))
                },
                error: function (response) {
                    alert(response.responseText);
                },
                failure: function (response) {
                    alert(response.responseText);
                }
            });
        },
        minLength: 1
   });
  }    
  </script> 

关于c# - Jquery 自动完成扩展程序在回发后无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12570839/

相关文章:

c# - 如何在 XAML 工具包 (WPF) 中的 Material Design 中激活汉堡菜单时禁用灰色覆盖

c# - C#.net 中的超时异常

javascript - 获取未选中复选框的值

c# - 在 C# 中以编程方式启动 HTTP 服务器?

jquery - 带有 Rails 的 format.js

javascript - 通过ajax传递时 header 不会重定向

c# - LINQ 或 foreach - 风格/可读性和速度

c# - 从 c# (SharePoint 2010) 使用 wkhtmltopdf

asp.net - 使用 apache 和 mod_mono 调用堆栈缺少关于单声道的信息

java - 我想在 Web 应用程序中进行身份验证并为桌面应用程序使用相同的凭据。我怎么做?