Gridview未在UpdatePanel内部更新

时间:2012-12-14 13:07:12

标签: asp.net vb.net gridview updatepanel

我知道这是一个非常常见的问题,我已经阅读了几天的文档了,我即将解决这个问题。

背景 我在UpdatePanel中有多个Gridview。发生了什么,有人正在导入Excel电子表格,我使用OpenXML来分解数据并将其存储在VB.NET数据表对象中。然后,我通过自定义验证(基于数据库信息)运行所有数据,然后根据异常发出Gridview中发生的异常(错误)。最大数量是一个UpdatePanel中的4个网格视图(每个Gridview都有自己的功能)。有两个Gridview我使用按钮来使用Gridview中包含的数据执行操作。这两个按钮也位于更新面板中,位于相应的Gridviews下方。每个Gridview都包含在AJAX可折叠面板扩展器中。

现在,当用户点击按钮时,我在后面的代码中有一个click事件,我在那里获取信息,并根据发生的异常,更新或插入数据库。我循环遍历行,如果没有错误发生,我调用datatable.ImportRow并将当前行传递给我的“Ready”表。我使用ScriptManager.RegisterStartupScript显示一个警告框,让他们知道是否发生任何错误。然后,我重新绑定异常表和“就绪”表。我试过添加一个AsyncPostbackTrigger,我试图在后面的代码中调用udpMain.Update(),并尝试将UpdatePanel的UpdateMode属性设置为“Always”和“Conditional”的两个选项。

HTML

    <asp:UpdatePanel ID="udpMain" runat="server" UpdateMode="Always">
<ContentTemplate>
    <asp:Panel ID="pnlOwnershipDetailsHead" runat="server" Visible="false">
                <div class="windHeader" style="cursor: pointer">
                    <asp:Label id="lblOwnershipDetails" runat="server">Ownership Exceptions</asp:Label>
                    <asp:ImageButton id="btnOwnershipHead" runat="server"/>
                </div>
              </asp:Panel>
               <asp:Panel ID="pnlOwnershipDetailsBody" runat="server" Visible="false" CssClass="pnl">
                <asp:GridView ID="gvOwnershipDetails" runat="server" CssClass="wind" CellPadding="5" AutoGenerateColumns="false">
                    <HeaderStyle CssClass="windHeader" />
                    <Columns>
                        <asp:BoundField DataField="Description" HeaderText="Description" />
                        <asp:BoundField DataField="Serial Number" HeaderText="Serial Number" />
                        <asp:BoundField DataField="Facility" HeaderText="Facility" />
                        <asp:BoundField DataField="Department" HeaderText="Department" />
                        <asp:BoundField DataField="EmpID" HeaderText="EmpID" />
                        <asp:BoundField DataField="Configuration" HeaderText="Config" />
                        <asp:BoundField DataField="Error" HeaderText="Errors" />
                        <asp:TemplateField>
                            <HeaderTemplate> 
                                <asp:CheckBox ID="chkHeader" ToolTip="Select All" runat="server" onclick="changeAllCheckBoxes(this)" />
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:CheckBox ID="chkItem" runat="server" ToolTip="Select this item" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                    <asp:Button ID="btnOwnershipDetails" Text="Change Information" runat="server" CssClass="btn editBtn" />
                    <ajax:ConfirmButtonExtender ID="cbeOwnershipDetails" runat="server" TargetControlID="btnOwnershipDetails"
                        ConfirmText="Are you sure you would like to change the ownership information for the selected items?"
                        OnClientCancel="CancelClick"  />
               </asp:Panel>
</ContentTemplate>
    <asp:UpdatePanel>

代码背后

 Protected Sub btnOwnershipDetails_Click(sender As Object, e As System.EventArgs) Handles btnOwnershipDetails.Click
        Dim importdata As New ImportData
        Dim ownershipdt As Data.DataTable = Session("ownershipdt")
        Dim finalimportdt As Data.DataTable = Session("finalimportdt")
        Dim existsError As Boolean = False

        For Each Row As Data.DataRow In ownershipdt.Rows
            Dim i As Integer = 0
            Dim cb As CheckBox = CType(gvOwnershipDetails.Rows(i).Cells(7).Controls(1), CheckBox)
            If cb.Checked Then
                If importdata.CheckEmpExists(Row("EmpID").ToString) And importdata.CheckSiteExists(Row("Facility").ToString) And importdata.CheckDeptExists(Row("Department").ToString) Then
                    importdata.UpdateDBOwnership(Row("Serial Number").ToString, ClientInfo.GetEmpID(Row("EmpID").ToString), ClientInfo.GetSiteID(Row("Facility").ToString), ClientInfo.GetDeptID(Row("Department").ToString), _
                                                Row("Description").ToString, Row("Configuration").ToString, portalUser.EmployeeText)

                    finalimportdt.ImportRow(Row)
                Else
                    existsError = True
                End If
            End If
            i += 1
        Next
        If existsError = False Then 'Show alert box
            ScriptManager.RegisterStartupScript(udpMain, udpMain.GetType(), "alert", "alert('You have changed the ownership information for the selected rows.')", True)
        Else
            ScriptManager.RegisterStartupScript(udpMain, udpMain.GetType(), "alert", "alert('There was an issue changing ownership to all of the selected rows.')", True)
        End If
        bindGV(gvOwnershipDetails, ownershipdt)
        bindGV(gvImportDetails, finalimportdt)
        'udpMain.Update()
        Session("ownershipdt") = ownershipdt
        Session("finalimportdt") = finalimportdt
        btnEmail.Enabled = True
    End Sub

3 个答案:

答案 0 :(得分:2)

将您的面板代码放在ASP:UpdatePanel

下的ContentTemplate之间
<asp:UpdatePanel>
    <ContentTemplate>

    </ContentTemplate>
</asp:UpdatePanel>

答案 1 :(得分:0)

答案 2 :(得分:0)

或者Page_Load()中的单行代码执行相同的操作

ScriptManager.GetCurrent(this).RegisterPostBackControl(btnOwnershipDetails);