ajax 自动完成功能不在 updatepanel 和模态弹出窗口中

发布于 2025-01-14 12:09:15 字数 4956 浏览 0 评论 0原文

我在模态中有一个 Ajax 自动完成文本框,模态位于 updatepanel 中,此代码在模态之外可以正常工作,但在位于模态中时不起作用,我认为使用 jquery 访问元素时存在问题,请帮助我 有我的 JQuery Ajax 代码:

 <script type="text/javascript">
    //On Page Load.
    $(function () {
        SetAutoComplete();
    });

    //On UpdatePanel Refresh.
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm != null) {
        prm.add_endRequest(function (sender, e) {
            if (sender._postBackSettings.panelsToUpdate != null) {
                SetAutoComplete();
            }
        });
    };
    function SetAutoComplete() {
        $("ContentPlaceHolder2_txtSearch").autocomplete({
       
      
            
       
            source: function (request, response) {
                $.ajax({
                    url: 'Unites.aspx/GetAutoCompleteTechnicalAssistance',
                    data: "{ 'name': '" + 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('-')[0],
                                val: item.split('-')[1]
                            };
                           
                        }))
                    }
                    
                });
                
            }
            
        });
      
    }
</script>

html 代码是:

        <asp:UpdatePanel ID="upModal" runat="server" UpdateMode="Conditional">
        <div class="modal fade" id="TechnicalModal">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">

                    <!-- Modal Header -->
                    <div class="modal-header btn-success">
                        <h4 class="modal-title">Register Technical Assistant</h4>
                        <button type="button" class="close" data-dismiss="modal">&times;</button>
                    </div>

                    <div class="card">
                        <div class="card-header myHeaderCard">Technical Assistant properties</div>

                        <div class="card-body">
                            <div class="form-group row" runat="server">
                                <label for="cmbusers" class="col-md-4 col-form-label mylabel">select user</label>
                                <div class="col-md-8">
                                    
                                    <asp:TextBox runat="server" ID="txtSearch" CssClass="autosuggest mycomboBox form-control"></asp:TextBox>
                                 
                                </div>

                            </div>
                        
                            <div class="card-footer myfooterCard">


                                <div class="text-center">
                                    <asp:Button Text="Insert" runat="server" ID="btnTechnicalInsert" CssClass="btn btn-success myBtn" Width="80px"
                                        OnClick="btnTechnicalInsert_Click" OnClientClick="dismissModal();" />
                                    <asp:Button Text="cancel" runat="server" Visible="false" ID="btnTechnicalCancel" CssClass="btn btn-warning"
                                        Width="90px" OnClick="btnTechnicalCancel_Click" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

       </asp:UpdatePanel>

c# 代码是:

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod()]
    public static List<string> GetAutoCompleteTechnicalAssistance(string name)
    {
        List<string> result = new List<string>();
    
        string connection = WebConfigurationManager.ConnectionStrings["strCnn"].ConnectionString;
        using (SqlConnection con = new SqlConnection(connection))
        {
           
            using (SqlCommand cmd = new SqlCommand("select DISTINCT id, name from TA where name LIKE '%'+@SearchText+'%'  ", con))
            {

                con.Open();
                cmd.Parameters.AddWithValue("@SearchText", name);
               
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    result.Add(dr["name"].ToString());
                }
                return result;
               
            }
        }
       
    }

提前谢谢

I have an Ajax autocomplete Textbox in modal and modal is located in updatepanel, This code works properly outside the modal But it does not work when located in modal ,I think there is a problem in accessing the elements with jquery, please help me
There is My JQuery Ajax Codes:

 <script type="text/javascript">
    //On Page Load.
    $(function () {
        SetAutoComplete();
    });

    //On UpdatePanel Refresh.
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm != null) {
        prm.add_endRequest(function (sender, e) {
            if (sender._postBackSettings.panelsToUpdate != null) {
                SetAutoComplete();
            }
        });
    };
    function SetAutoComplete() {
        $("ContentPlaceHolder2_txtSearch").autocomplete({
       
      
            
       
            source: function (request, response) {
                $.ajax({
                    url: 'Unites.aspx/GetAutoCompleteTechnicalAssistance',
                    data: "{ 'name': '" + 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('-')[0],
                                val: item.split('-')[1]
                            };
                           
                        }))
                    }
                    
                });
                
            }
            
        });
      
    }
</script>

html code is:

        <asp:UpdatePanel ID="upModal" runat="server" UpdateMode="Conditional">
        <div class="modal fade" id="TechnicalModal">
            <div class="modal-dialog modal-dialog-centered">
                <div class="modal-content">

                    <!-- Modal Header -->
                    <div class="modal-header btn-success">
                        <h4 class="modal-title">Register Technical Assistant</h4>
                        <button type="button" class="close" data-dismiss="modal">×</button>
                    </div>

                    <div class="card">
                        <div class="card-header myHeaderCard">Technical Assistant properties</div>

                        <div class="card-body">
                            <div class="form-group row" runat="server">
                                <label for="cmbusers" class="col-md-4 col-form-label mylabel">select user</label>
                                <div class="col-md-8">
                                    
                                    <asp:TextBox runat="server" ID="txtSearch" CssClass="autosuggest mycomboBox form-control"></asp:TextBox>
                                 
                                </div>

                            </div>
                        
                            <div class="card-footer myfooterCard">


                                <div class="text-center">
                                    <asp:Button Text="Insert" runat="server" ID="btnTechnicalInsert" CssClass="btn btn-success myBtn" Width="80px"
                                        OnClick="btnTechnicalInsert_Click" OnClientClick="dismissModal();" />
                                    <asp:Button Text="cancel" runat="server" Visible="false" ID="btnTechnicalCancel" CssClass="btn btn-warning"
                                        Width="90px" OnClick="btnTechnicalCancel_Click" />
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

       </asp:UpdatePanel>

and c# code is :

    [WebMethod]
    [System.Web.Script.Services.ScriptMethod()]
    public static List<string> GetAutoCompleteTechnicalAssistance(string name)
    {
        List<string> result = new List<string>();
    
        string connection = WebConfigurationManager.ConnectionStrings["strCnn"].ConnectionString;
        using (SqlConnection con = new SqlConnection(connection))
        {
           
            using (SqlCommand cmd = new SqlCommand("select DISTINCT id, name from TA where name LIKE '%'+@SearchText+'%'  ", con))
            {

                con.Open();
                cmd.Parameters.AddWithValue("@SearchText", name);
               
                SqlDataReader dr = cmd.ExecuteReader();

                while (dr.Read())
                {
                    result.Add(dr["name"].ToString());
                }
                return result;
               
            }
        }
       
    }

thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长亭外,古道边 2025-01-21 12:09:15

删除 UpdateMode="Conditional"

PanelScript 上有一个更新块触发器

Remove UpdateMode="Conditional"

The PanelScript has an update block trigger on it

沉默的熊 2025-01-21 12:09:15

我通过更改面板覆盖的区域并添加缺少的 来包装面板内容来更正您的代码。检查它是否适合您。

<div class="modal fade" id="TechnicalModal">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">

            <!-- Modal Header -->
            <div class="modal-header btn-success">
                <h4 class="modal-title">Register Technical Assistant</h4>
                <button type="button" class="close" data-dismiss="modal">×</button>
            </div>

            <div class="card">
                <div class="card-header myHeaderCard">Technical Assistant properties</div>

                <div class="card-body">

                    <asp:UpdatePanel ID="upModal" runat="server">
                        <ContentTemplate>

                            <div class="form-group row" runat="server">
                                <label for="cmbusers" class="col-md-4 col-form-label mylabel">select user</label>
                                <div class="col-md-8">
                                    <asp:TextBox runat="server" ID="txtSearch" CssClass="autosuggest mycomboBox form-control"></asp:TextBox>
                                </div>
                            </div>
                            <div class="card-footer myfooterCard">
                                <div class="text-center">
                                    <asp:Button Text="Insert" runat="server" ID="btnTechnicalInsert" CssClass="btn btn-success myBtn" Width="80px"
                                        OnClick="btnTechnicalInsert_Click" OnClientClick="dismissModal();" />
                                    <asp:Button Text="cancel" runat="server" Visible="false" ID="btnTechnicalCancel" CssClass="btn btn-warning"
                                        Width="90px" OnClick="btnTechnicalCancel_Click" />
                                </div>
                            </div>

                        </ContentTemplate>
                    </asp:UpdatePanel>

                </div>
            </div>
        </div>
    </div>
</div>

I corrected your code by changing area covered with panel and adding missing <ContentTemplate> to wrap panel content. Check if it works for you.

<div class="modal fade" id="TechnicalModal">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">

            <!-- Modal Header -->
            <div class="modal-header btn-success">
                <h4 class="modal-title">Register Technical Assistant</h4>
                <button type="button" class="close" data-dismiss="modal">×</button>
            </div>

            <div class="card">
                <div class="card-header myHeaderCard">Technical Assistant properties</div>

                <div class="card-body">

                    <asp:UpdatePanel ID="upModal" runat="server">
                        <ContentTemplate>

                            <div class="form-group row" runat="server">
                                <label for="cmbusers" class="col-md-4 col-form-label mylabel">select user</label>
                                <div class="col-md-8">
                                    <asp:TextBox runat="server" ID="txtSearch" CssClass="autosuggest mycomboBox form-control"></asp:TextBox>
                                </div>
                            </div>
                            <div class="card-footer myfooterCard">
                                <div class="text-center">
                                    <asp:Button Text="Insert" runat="server" ID="btnTechnicalInsert" CssClass="btn btn-success myBtn" Width="80px"
                                        OnClick="btnTechnicalInsert_Click" OnClientClick="dismissModal();" />
                                    <asp:Button Text="cancel" runat="server" Visible="false" ID="btnTechnicalCancel" CssClass="btn btn-warning"
                                        Width="90px" OnClick="btnTechnicalCancel_Click" />
                                </div>
                            </div>

                        </ContentTemplate>
                    </asp:UpdatePanel>

                </div>
            </div>
        </div>
    </div>
</div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文