Jag*_*agd 5 asp.net jquery 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在异步回发期间会更新很好(参见上面的点击事件); 然而,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);
    }
}
小智 2
用于 jQuery UI 对话框的 div 应该位于 UpdatePanel 之外。
创建btnAddSupplier_Click对话框并将其移到 UpdatePanel 之外。
当你有 2 个带有 divSuppliers id 的 div 后,移动的一个和从服务器接收的btnSelectSupplier_Click一个(UpdatePanel 机制允许通过使用从服务器返回的 html 重建整个 UpdatePanel DOM 来更新部分页面)。
我还建议使用console.log来帮助调试。
添加到关闭对话框js:console.log($('#divSuppliers'))
| 归档时间: | 
 | 
| 查看次数: | 3506 次 | 
| 最近记录: |