vb.net Gridview 不在 UpdatePanel 内更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13879298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:33:48  来源:igfitidea点击:

Gridview is not updating inside of UpdatePanel

asp.netvb.netgridviewupdatepanel

提问by Josh McKearin

I understand that this is a very common issue, I have been reading documentation on it for days, and I am about to pull my hair out over this issue.

我知道这是一个非常普遍的问题,我几天来一直在阅读有关它的文档,并且我将要解决这个问题。

BACKGROUNDI have multiple Gridviews inside of an UpdatePanel. What is happening, is someone is importing an Excel spreadsheet, I am using OpenXML to shread the data and store it in a VB.NET datatable object. I then run all of that data through a custom validation (based on DB information) and then spit out the exceptions (errors) that occur into a Gridview depending on the exception. the max number is 4 Gridviews in one UpdatePanel (each Gridview has it's own functionality). There are two Gridviews that I am using a button to do an action using the data contained in the Gridview. Those two buttons are also located in the Update Panel, just underneath the corresponding Gridviews. Each Gridview is wrapped in an AJAX Collapsible Panel Extender.

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

Now, when the user clicks on the button, I have a click event in the code behind where I take the information and depending on the exception that occurred, Update or Insert the DB. I loop through the rows, and if no error occurs, I call the datatable.ImportRow and pass the current row to my "Ready" table. I use a ScriptManager.RegisterStartupScript in order to display an alert box letting them know if any errors occurred. Then, I rebind the exception table and the "Ready" table. I have tried just adding an AsyncPostbackTrigger, I have attempted simply calling udpMain.Update() in the code behind, and tried both options of setting the UpdatePanel's UpdateMode property to "Always" and "Conditional".

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

HTML

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>

CODE BEHIND

代码隐藏

 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

回答by Naresh Pansuriya

Put your panel code between ContentTemplate under asp:UpdatePanel

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

<asp:UpdatePanel>
    <ContentTemplate>

    </ContentTemplate>
</asp:UpdatePanel>

回答by Fandango68

Or a single line of code in the Page_Load() does the same thing

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

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