ModalpopupExtender没有隐藏,而且Gridview没有刷新

时间:2010-10-08 15:54:14

标签: c# asp.net gridview modalpopupextender detailsview

我在UpdatePanel中有一个Gridview,它显示了我数据库中的一些数据。当您单击编辑按钮时,它会在ModalPopupextender中打开detailsView。当您在此detailView中的textBoxes中输入数据并单击“更新”时,数据库将更新,但弹出窗口不会隐藏。然后,当我通过单击“关闭”手动关闭它时,除非我刷新页面,否则gridView不会刷新。我以前能够让这个工作起来,但在花了一个星期盯着这个问题之后,我决定让你们看看我做错了什么。

这是一些代码! (注意:很多这是我发现的教程的改编)

<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:GridView ID="gvReservations" runat="server" CssClass="datagrid" DataKeyNames="dateSubmit"
                AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" PageSize="10"
                DataSourceID="mainTable" Width="95%" OnRowUpdated="gvReservation_RowUpdated"  OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="GvReservations_SelectedIndexChanged">
                <Columns>
                    <asp:BoundField DataField="dateSubmit" HeaderText="Date Submitted" SortExpression="dateSubmit"
                        ReadOnly="true" />
                    <asp:BoundField DataField="lName" HeaderText="Name" SortExpression="lName" ReadOnly="true" />
                    <asp:BoundField DataField="startTime" HeaderText="Start Time" SortExpression="startTime"
                        ReadOnly="true" />
                    <asp:BoundField DataField="endTime" HeaderText="End Time" SortExpression="endTime"
                        ReadOnly="true" />
                    <asp:BoundField DataField="labName" HeaderText="Lab" SortExpression="labName" ReadOnly="true" />
                    <asp:BoundField DataField="class" HeaderText="Class" SortExpression="class" ReadOnly="true" />
                    <asp:BoundField DataField="term" HeaderText="Semester" SortExpression="term" ReadOnly="true" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:LinkButton ID="btnViewDetails" runat="server" Text="more" CommandName="Select" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Panel ID="pnlPopup" runat="server" CssClass="detail" Width="500px" Style="display: none;">
        <asp:UpdatePanel ID="updPnlReservationDetail" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Button ID="btnShowPopup" runat="server" Style="display: none" />
                <ajaxToolkit:ModalPopupExtender ID="mdlPopup" runat="server" TargetControlID="btnShowPopup"
                    PopupControlID="pnlPopup" CancelControlID="btnClose"  BackgroundCssClass="modalBackground" />
                <asp:DetailsView ID="dvReservationDetail" runat="server" DataSourceID="SqlDetail"
                   OnDataBound="ReservationDetail_DataBound"  CssClass="detailgrid" GridLines="None" DefaultMode="Edit" AutoGenerateRows="false"
                      Visible="false" Width="100%" OnItemUpdating="ReservationDetail_Updating"   OnItemUpdated="ReservationDetail_Updated">
                    <Fields>

                    <asp:TemplateField HeaderText="ID">
                            <EditItemTemplate>
                                <asp:TextBox ID="tbID" runat="server" Text='<%# Bind("id") %>' ReadOnly="true" />                                    
                            </EditItemTemplate>
                        </asp:TemplateField>

                        <asp:BoundField HeaderText="LabName" DataField="labName" />
                        <asp:BoundField  HeaderText="DateSubmitted" DataField="dateSubmit" />

                         <asp:TemplateField HeaderText="Start Time">
                            <EditItemTemplate>
                               <asp:TextBox ID="startTime" runat="server" Text='<%# Bind("startTime") %>' />                                    
                            </EditItemTemplate>
                        </asp:TemplateField>


                        <asp:TemplateField HeaderText="End Time">
                            <EditItemTemplate>
                                <asp:TextBox ID="endTime" runat="server" Text='<%# Bind("endTime") %>' />                                    
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ShowEditButton="true" />

                    </Fields>
                </asp:DetailsView>

                <div class="footer">

                    <asp:LinkButton ID="btnClose" runat="server" Text="Close" CausesValidation="false" />
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </asp:Panel>

这些是我认为会关闭弹出窗口并更新gridView

的事件
protected void ReservationDetail_Updated(object sender, DetailsViewUpdatedEventArgs e)
    {
        if (this.Page.IsValid)
        {

            //  move the data back to the data object
           // this.dvReservationDetail.UpdateItem(false);
            dvReservationDetail.Visible = false;

            //  hide the modal popup
            mdlPopup.Hide();

            //  refresh the grid

            this.gvReservations.DataBind();
            this.updatePanel.Update();
        }

    }

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

尝试在页面上使用单个更新面板 将所有控件放在里面。                                                                                                                                                                                                                                                                                                                                                 

<asp:Panel ID="pnlPopup" runat="server" CssClass="detail" Width="500px" Style="display: none;">
            <asp:Button ID="btnShowPopup" runat="server" Style="display: none" />
            <ajaxToolkit:ModalPopupExtender ID="mdlPopup" runat="server" TargetControlID="btnShowPopup"
                PopupControlID="pnlPopup" CancelControlID="btnClose"  BackgroundCssClass="modalBackground" />
            <asp:DetailsView ID="dvReservationDetail" runat="server" DataSourceID="SqlDetail"
               OnDataBound="ReservationDetail_DataBound"  CssClass="detailgrid" GridLines="None" DefaultMode="Edit" AutoGenerateRows="false"
                  Visible="false" Width="100%" OnItemUpdating="ReservationDetail_Updating"   OnItemUpdated="ReservationDetail_Updated">
                <Fields>

                <asp:TemplateField HeaderText="ID">
                        <EditItemTemplate>
                            <asp:TextBox ID="tbID" runat="server" Text='<%# Bind("id") %>' ReadOnly="true" />                                    
                        </EditItemTemplate>
                    </asp:TemplateField>

                    <asp:BoundField HeaderText="LabName" DataField="labName" />
                    <asp:BoundField  HeaderText="DateSubmitted" DataField="dateSubmit" />

                     <asp:TemplateField HeaderText="Start Time">
                        <EditItemTemplate>
                           <asp:TextBox ID="startTime" runat="server" Text='<%# Bind("startTime") %>' />                                    
                        </EditItemTemplate>
                    </asp:TemplateField>


                    <asp:TemplateField HeaderText="End Time">
                        <EditItemTemplate>
                            <asp:TextBox ID="endTime" runat="server" Text='<%# Bind("endTime") %>' />                                    
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:CommandField ShowEditButton="true" />

                </Fields>
            </asp:DetailsView>

            <div class="footer">

                <asp:LinkButton ID="btnClose" runat="server" Text="Close" CausesValidation="false" />
            </div>            
</asp:Panel>