jQuery对话'关闭'与ASP.NET UpdatePanel的冲突

时间:2012-05-07 18:14:43

标签: jquery asp.net updatepanel

我有一个ASP.NET UpdatePanel,其中包含以下内容:

<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
<ContentTemplate>
    <%-- jQuery dialog - Suppliers --%>
    <div id="divSuppliers" style="display: none;">
        <asp:ListBox ID="lstSuppliers" runat="server" SelectionMode="Single" Rows="10" Width="100%"
            DataValueField="SupplierID" DataTextField="SupplierName">
        </asp:ListBox>
        <br /><br />
        <asp:Button ID="btnSelectSupplier" runat="server" Text="Select 2" OnClick="btnSelectSupplier_Click" />
    </div>

    <asp:GridView ID="gvSuppliers" runat="server" AutoGenerateColumns="false" SkinID="gvSkin"
        DataKeyNames="SupplierID" EmptyDataText="No Existing User Roles">
        <Columns>
            <asp:TemplateField HeaderText="Supplier Name">
                <ItemTemplate>
                    <asp:Label ID="lblSupplierName" runat="server" Text='<%# Eval("SupplierName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    <asp:Button ID="btnAddSupplier" runat="server" Text="Add Supplier" 
        Visible="false" OnClick="btnAddSupplier_Click" />

</ContentTemplate>
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="btnSelectSupplier" />
</Triggers>
</asp:UpdatePanel>

非常简单,真的。只有我用于我的jQuery Dialog弹出窗口的div,带有单列的ASP.NET GridView控件和用于异步回发的ASP.NET按钮。

这是处理btnSelectSupplier的异步回发的click事件。

protected void btnSelectSupplier_Click(object sender, EventArgs e) {

    // +=+=+=+=+=+=+=+=+=+=+=+=+=+=    
    // WORKS JUST FINE
    List<SupplierItem> suppliers = new List<SupplierItem>();    

    foreach (int i in lstSuppliers.GetSelectedIndices()) {
        suppliers.Add(
            new SupplierItem { SupplierID = Convert.ToInt32(lstSuppliers.Items[i].Value), SupplierName = lstSuppliers.Items[i].Text });
        lstSuppliers.Items[i].Selected = false;
    }

    gvSuppliers.DataSource = suppliers;
    gvSuppliers.DataBind();

    // +=+=+=+=+=+=+=+=+=+=+=+=+=+=
    // DOES NOT WORK!!
    string jq = "$('#divSuppliers').dialog('close');";

    ScriptManager sm = ScriptManager.GetCurrent(this);
    if (sm != null && sm.IsInAsyncPostBack) {
        ScriptManager.RegisterClientScriptBlock(
            this, typeof(Page), Guid.NewGuid().ToString(),
            jq, true);
    }
}

问题: GridView将在异步回发期间更新正常(参见上面的click事件);然而,jQuery Dialog拒绝关闭(再次,看到上面的事件和它说不工作的地方)。我正在页面上使用ScriptManager注册javascript(jquery),因此它应该执行并关闭对话框,但由于某种原因它没有。

修改 打开jQuery对话框并使其成为模态的代码。

protected void btnAddSupplier_Click(object sender, EventArgs e) {
    lstSuppliers.ClearSelection();
    lstSuppliers.DataSource = Suppliers.GetAllSuppliers();
    lstSuppliers.DataBind();

    string jq = "var dlg = $('#divSuppliers').dialog({ modal: true, draggable: true, title: 'Suppliers', width: 500 }); dlg.parent().appendTo($('form:first'));";

    ScriptManager sm = ScriptManager.GetCurrent(this);
    if (sm != null && sm.IsInAsyncPostBack) {
        ScriptManager.RegisterClientScriptBlock(
            this, typeof(Page), Guid.NewGuid().ToString(),
            jq, true);
    }
}

3 个答案:

答案 0 :(得分:2)

用于jQuery UI Dialog的div应该在UpdatePanel之外。

btnAddSupplier_Click上创建对话框并将其移到UpdatePanel之外 在btnSelectSupplier_Click之后你有2个带有divSuppliers id的div,移动的一个和received from server(UpdatePanel机制允许通过从服务器返回的html重建整个UpdatePanel DOM来进行部分页面更新)。

我还建议使用console.log来帮助调试 添加到关闭对话框js:console.log($('#divSuppliers'))

答案 1 :(得分:1)

您是否考虑过更简单的路由并将事件绑定到按钮客户端以关闭对话框?

 $(function () {
    $('#btnSelectSupplier').click(function () {

        $('#divSuppliers').dialog('close');
    });
});

从用户的角度来看,流程仍然相同。他们单击按钮,对话框关闭。服务器端代码将在对话框关闭后运行,但由于似乎没有任何服务器端逻辑决定是否要在单击后关闭对话框,这可能符合您的需求。

编辑我看到了您的问题。您遇到了一个问题,因为您不是打开现有的对话框,而是在点击时重新定义对话框:

 string jq = "var dlg = $('#divSuppliers').dialog({ modal: true, draggable: true, title: 'Suppliers', width: 500 }); dlg.parent().appendTo($('form:first'));";

相反,您需要在document.ready函数中定义对话框。

 $(function () {
    //define the div as a dialog. By default, it will not open until you tell it to
$('#divSuppliers').dialog({ modal: true, draggable: true, title: 'Suppliers', width: 500 });

});

点击后,你应该像这样打开它

 $('#divSuppliers').dialog('open');

您可能希望再次执行此客户端,而不是尝试将脚本推送到服务器端事件的页面,但这取决于您。

最简单的完整解决方案:

$(function () {

$('#divSuppliers').dialog({ modal: true, draggable: true, title: 'Suppliers', width: 500 });

$('#btnAddSupplier').click(function () {

    $('#divSuppliers').dialog('close');
});
$('#btnSelectSupplier').click(function () {

    $('#divSuppliers').dialog('open');
});
});

答案 2 :(得分:0)

所以这里是解决方案:不要尝试使用ASP.NET UpdatePanel与jQuery Dialog混合使用,因为它们不能很好地协同工作。

我只是走了这个方向,因为我认为通过使用.NET的ScriptManager和UpdatePanel控件而不是在jQuery中完成所有操作,我将花费更少的时间来实现AJAX。在这一点上,我似乎错了,因为我要回去并扯掉.NET垃圾并用jQuery替换它。所以那段时间,我以为我会保存,就像风一样。

故事的道德:如果你不需要,不要混淆两者。