更新面板中的异步回发后,JQuery UI 模式对话框未关闭
我在 ASP.Net 应用程序中使用 jQuery UI 的模式对话框。
该页面包含用于异步回发的UpdatePanel 组件。
aspx code
<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 code
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');没有做需要做的事情。
有什么想法如何解决这个问题吗?
I am using modal dialog of jQuery UI with my ASP.Net application.
The page contains UpdatePanel component for asynchronous postbacks.
aspx code
<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 code
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);
}
When button Add is clicked the codebehind prepares controls on dialog (putting there values or removing them, it depends on the fact if dialog is used to add item or to edit it) and then sends script to create and show dialog. When Save button is clicked the code behind (after omitted here routine of checking the data entry, storing it and rebinding the repeater) sends script to close the dialog. The problem is that the dialog is not closing. The JavaScript function CloseDialog is called but $('#modal).dialog('close'); does not do what is required to.
Any ideas how to solve this problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的代码对我来说效果很好:
代码隐藏:
The code below works well for me:
Code-behind:
我自己解决了。
显示/关闭对话框的脚本是这样的:
我所做的是:添加到关闭函数对话框的定义:
现在对话框正在毫无问题地关闭。
但!!!
Yuriy Rozhovetskiy 的贡献非常有价值:将 UpdatePanel 添加到对话框 DIV 的内容解决了从代码隐藏更新对话框的许多其他问题。我相信,在 UpdatePanel 中使用 jQuery UI 对话框的最佳解决方案是在对话框中再添加一个 UpdatePanel。谢谢,尤里!
Solved it myself.
The script to show/close dialog was like that:
What I did is: add to close function the definition of dialog:
And now dialog is closing without problem.
BUT!!!
The contribution of Yuriy Rozhovetskiy was extremely valuable: adding UpdatePanel to the content of dialog's DIV solved a lot of other issues of updating the dialog from codebehind. I believe, the best solution for using jQuery UI dialog inside UpdatePanel is to add one more UpdatePanel inside dialog. Thanks, Yuriy !!!