【问题标题】:JQuery UI modal dialog is not closing after asyncpostback in update panel更新面板中的异步回发后,JQuery UI 模式对话框未关闭
【发布时间】:2011-10-19 14:09:26
【问题描述】:

我在 ASP.Net 应用程序中使用 jQuery UI 的模态对话框。

该页面包含用于异步回发的 UpdatePanel 组件。

aspx 代码

                <script>
                   function ShowDialog() {
                      $('#modal').dialog({
                         autoOpen: false,
                         modal: true,
                         dialogClass: 'dialog',
                         resizable: false,
                         width: 500,
                         height: 400
                      });
                      $('#modal).dialog('open');
                   }

                   function CloseDialog(){
                      $('#modal).dialog('close');
                   }
                </script>
                <asp:UpdatePanel ID="updatepanel" runat="server" RenderMode="Inline">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="btnSave" EventName="Click" />
                </Triggers>
                <ContentTemplate>
                    <asp:Repeater ID="repEducationHistory" runat="server">
                        <HeaderTemplate>
                            <table>
                                <tr>
                                    <th>Field 1</th>
                                    <th>Field 2</th>
                                </tr>                                            
                        </HeaderTemplate>
                        <ItemTemplate>
                             <tr>
                                <td><asp:Literal ID="litField1" runat="server">
                                   </asp:Literal>
                                </td>
                                <td><asp:Literal ID="litField1" runat="server">
                                   </asp:Literal>
                                </td>
                             </tr>
                        </ItemTemplate>
                        <FooterTemplate>
                           </table>
                        </FooterTemplate>
                    </asp:Repeater>

                    <asp:LinkButton ID="btnAdd" runat="server" Text="Add new"
                       OnClick="BtnAdd_Click" />

                    <div id="modal" title="Add item" style="display:none;">
                        <div>
                            <label>Educational institution:</label>
                            <asp:TextBox ID="tbField1" runat="server"/>
                            <br />
                            <label>Country:</label>
                            <asp:DropDownList ID="tbField2" runat="server" 
                               DataValueField="Id" DataTextField="Name" />
                            <br />
                            <asp:LinkButton ID="btnSave" runat="server" 
                               Text="Save" OnClick="BtnSave_Click"></asp:LinkButton>
                        </div>
                    </div>
                    <asp:LinkButton ID="btnSave" runat="server" CausesValidation="true"
                         ValidationGroup="vgMpt" Text="Save" OnClick="BtnSaveUsrEdu_Click">
                    </asp:LinkButton>

cs代码

   protected void BtnAdd_Click(object sender, EventArgs e)
    {
        tbField1.Text=string.Empty;
        ddlField2.SelectedIndex=0;
        ScriptManager.RegisterStartupScript(updatepanel, updatepanel.GetType(), 
           Guid.NewGuid().ToString(), "ShowDialog();", true);
    }
    protected void BtnSaveUsrEdu_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(updatepanel, updatepanel.GetType(), 
           Guid.NewGuid().ToString(), "CloseDialog();", true);
    }

单击“添加”按钮时,代码隐藏在对话框上准备控件(放置值或删除它们,这取决于对话框是用于添加项目还是用于编辑它),然后发送脚本以创建和显示对话框。当单击保存按钮时,后面的代码(在此处省略了检查数据输入、存储和重新绑定中继器的例程之后)发送脚本以关闭对话框。问题是对话框没有关闭。 JavaScript 函数 CloseDialog 被调用,但 $('#modal).dialog('close');不做需要做的事情。

任何想法如何解决这个问题?

【问题讨论】:

  • 为什么不对按钮的 OnClientClick 事件调用 CloseDialog?
  • 因为首先我需要检查代码隐藏中的条目。当然,会有客户端验证器,但是在不控制代码隐藏的情况下处理数据是不安全的。另外,我需要检查条目是否已经存在。在所有这些情况下,我需要在回发后保留对话框以让用户调整条目。
  • 你能检查一下标记吗?有带有 btnSave ID 的脚趾链接按钮。您想使用哪一个来关闭对话框?它看起来像模态 div 中的 LinkBut​​ton。对吗?

标签: asp.net jquery-ui dialog


【解决方案1】:

下面的代码很适合我:

<asp:UpdatePanel ID="updatepanel" runat="server" UpdateMode="Conditional" RenderMode="Inline">
     <Triggers>
          <asp:AsyncPostBackTrigger ControlID="btnSave" />
     </Triggers>
     <ContentTemplate>
          <asp:LinkButton ID="btnAdd" runat="server" Text="Add new" OnClick="BtnAdd_Click" />
     </ContentTemplate>
</asp:UpdatePanel>
<div id="modal" title="Add item" style="display: none;">
     <asp:UpdatePanel runat="server" ID="DialogUpdatePanel">
          <ContentTemplate>
               <div>
                    <label>
                         Educational institution:
                         <asp:TextBox ID="tbField1" runat="server" />
                    </label>
                    <br />
                    <label>
                         Country:
                         <asp:DropDownList ID="tbField2" runat="server" DataValueField="Id" DataTextField="Name" />
                    </label>
                    <br />
                    <asp:LinkButton ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click"></asp:LinkButton>
               </div>
          </ContentTemplate>
     </asp:UpdatePanel>
</div>

代码隐藏:

protected void BtnAdd_Click(object sender, EventArgs e)
{
    tbField1.Text = string.Empty;
    ScriptManager.RegisterStartupScript(updatepanel, updatepanel.GetType(),
        Guid.NewGuid().ToString(), "ShowDialog();", true);
}
protected void btnSave_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(DialogUpdatePanel, DialogUpdatePanel.GetType(),
        Guid.NewGuid().ToString(), "CloseDialog();", true);
}

【讨论】:

  • 我明白了。您的意思是-将更新面板添加到对话框?唔。我会试试看它是否满足对话框关闭之外的所有需求。无论如何 - spasibo bolshoye
【解决方案2】:

自己解决了。

显示/关闭对话框的脚本是这样的:

           function ShowDialog() { 
              $('#modal').dialog({ 
                 autoOpen: false, 
                 modal: true, 
                 dialogClass: 'dialog', 
                 resizable: false, 
                 width: 500, 
                 height: 400 
              }); 
              $('#modal).dialog('open'); 
           } 

           function CloseDialog(){ 
              $('#modal).dialog('close'); 
           } 

我所做的是:将对话框的定义添加到关闭函数中:

           function CloseDialog(){ 
              $('#modal').dialog({ 
                 autoOpen: false, 
                 modal: true, 
                 dialogClass: 'dialog', 
                 resizable: false, 
                 width: 500, 
                 height: 400 
              }); 
              $('#modal).dialog('close'); 
           }

现在对话框正在关闭,没有问题。

但是!!!

Yuriy Rozhovetskiy 的贡献非常有价值:将 UpdatePanel 添加到对话框的 DIV 内容中解决了许多其他从代码隐藏更新对话框的问题。我相信,在 UpdatePanel 中使用 jQuery UI 对话框的最佳解决方案是在对话框中再添加一个 UpdatePanel。谢谢,尤里!!!

【讨论】:

  • 我只想指出,如果您按照 Yuriy 的建议使用单独的 UpdatePanel,则不需要像上面的 CloseDialog 那样重新创建对话框。 (如果不清楚。)
最近更新 更多