【问题标题】:Gridview is not updating inside of UpdatePanelGridview 没有在 UpdatePanel 内部更新
【发布时间】:2012-12-14 13:07:11
【问题描述】:

我知道这是一个非常常见的问题,我已经阅读了有关它的文档好几天了,我正要为这个问题大发雷霆。

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

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

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

【问题讨论】:

  • 删除更新面板以查看它是否有效,如果无效,您会遇到什么错误。
  • 没有错误,但它也没有工作。如果没有 UpdatePanel,要更新 Gridview,我必须正确发送完整的回发?
  • 首先让更新面板远离您的代码,直到您在这里找到真正的问题 - 现在标题是错误的,UpdatePanel 不是您的问题
  • 如我所说,我已删除 UpdatePanel。一切都执行没有错误,但 Gridview 仍然没有更新。我不明白标题是怎么错的。我可以在没有 UpdatePanel 和完整回发的情况下整天完成这项工作。问题是我无法使用部分回发使其在 UpdatePanel 中正确运行。

标签: asp.net vb.net gridview updatepanel


【解决方案1】:

把你的面板代码放在asp:UpdatePanel下的ContentTemplate之间

<asp:UpdatePanel>
    <ContentTemplate>

    </ContentTemplate>
</asp:UpdatePanel>

【讨论】:

    【解决方案2】:

    【讨论】:

      【解决方案3】:

      或者 Page_Load() 中的一行代码做同样的事情

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-08
        • 2012-02-22
        相关资源
        最近更新 更多