首页 文章

jQuery Dialog 'Close'与ASP.NET UpdatePanel冲突

提问于
浏览
5

我有一个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>
        

<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);
    }
}

PROBLEM: GridView在异步回发期间会更新正常(参见上面的点击事件);然而,jQuery Dialog拒绝关闭(再次,看到上面的事件和它说不工作的地方) . 我'm registering the javascript (jquery) with the ScriptManager on the page and so it should be executing and closing the dialog, but for some reason it doesn' t .

EDIT: 打开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 回答

  • 2

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

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

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

  • 0

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

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

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

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

    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');
    

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

    Simplest full solution:

    $(function () {
    
    $('#divSuppliers').dialog({ modal: true, draggable: true, title: 'Suppliers', width: 500 });
    
    $('#btnAddSupplier').click(function () {
    
        $('#divSuppliers').dialog('close');
    });
    $('#btnSelectSupplier').click(function () {
    
        $('#divSuppliers').dialog('open');
    });
    });
    
  • 1

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

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

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

相关问题