ASP GridView Refresh after Excel Export not working

前提是你 提交于 2019-12-22 18:46:05

问题


I have a Gridview inside an Ajax UpdatePanel. Inside each GV row I have a checkbox field. The idea is that the user checks the lines they want, and click a button to both update a label in that row as "shipped" and then also export the checked lines to an xls file (csv really).

When my codebehind fires it loops through the gridview rows, looks for the checks, updates the database to mark each line and then I use .DataBind() to refresh the grid. This works perfectly and as expected.

Now I want to also export the checked rows to excel. So I created a method for doing this and popped it in after updating each row for marking the lines and BEFORE the .DataBind() refresh. Now the .DataBind() never fires to refresh. I do receive the XLS download and if I manually refresh the screen, the lines are updated as expected.

Here's my code:

ASPX (Just a portion of the gridview):
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                DataKeyNames="MinqNum" DataSourceID="SqlDataSource1" Font-Size="Small" BorderColor="Black"
                BorderStyle="Solid" BorderWidth="1px" CellPadding="0">
                <RowStyle Font-Size="Small" HorizontalAlign="Left" VerticalAlign="Bottom" BorderColor="#999999"
                    BorderStyle="Solid" BorderWidth="1px" Wrap="true" />
                <Columns>
                    <asp:TemplateField>
                    <ItemStyle CssClass="ItemStyle"/>
                    <HeaderStyle Wrap="true" Font-Size="X-Small" HorizontalAlign="Center"
                            VerticalAlign="Bottom" BorderWidth="0px" />
                        <ItemTemplate>
                            <asp:ImageButton ID="btn_editss" runat="server" CommandName="Edit" ImageUrl="~/images/edit.gif" />
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:ImageButton ID="btn_savess" runat="server" CommandName="Update" ImageUrl="~/images/save.gif" />
                            <asp:ImageButton ID="btn_cancelss" runat="server" CommandName="Cancel" ImageUrl="~/images/cancel.gif" />
                        </EditItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Date" SortExpression="MinqDate">
                        <ItemStyle CssClass="ItemStyle" HorizontalAlign="Center" Font-Size="Smaller"/>
                        <HeaderStyle Wrap="true" Font-Size="X-Small" HorizontalAlign="Center" VerticalAlign="Bottom" BorderWidth="0px"/>
                        <ItemTemplate>
                            <asp:Label ID="lbl_minqdate" runat="server" Text='<%#Bind("MinqDate", "{0:MM/dd/yy}") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:Label ID="lbl_minqdate" runat="server" Text='<%#Bind("MinqDate", "{0:MM/dd/yy}") %>'></asp:Label>
                        </EditItemTemplate>
                    </asp:TemplateField>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>


Button Click Event (codebehind):
Protected Sub btn_markshipped_clicked(ByVal sender As Object, ByVal e As EventArgs)
    For Each row As GridViewRow In GridView1.Rows
        If row.RowType = DataControlRowType.DataRow Then
            Dim isChecked As Boolean = DirectCast(row.FindControl("cb_supship"), CheckBox).Checked
            If isChecked Then
                'btn_markshipped.Text = "changed"
                Dim cmd As New SqlCommand("UPDATE InquiryV4.dbo.Main SET Sup_Shipped = 'S' WHERE MinqNum = @MinqNum")
                cmd.Parameters.AddWithValue("@MinqNum", row.Cells(5).Controls.OfType(Of Label)().FirstOrDefault().Text)
                'cmd.Parameters.AddWithValue("@Country", row.Cells(2).Controls.OfType(Of DropDownList)().FirstOrDefault().SelectedItem.Value)
                'cmd.Parameters.AddWithValue("@CustomerId", gvCustomers.DataKeys(row.RowIndex).Value)
                Me.ExecuteQuery(cmd, "UPDATE")
            End If
        End If
    Next
    'btnUpdate.Visible = False
    'Me.BindGrid()
    btn_exportexcel()
    GridView1.DataBind()
End Sub

btn_exportexcel sub (codebehind):
    Private Sub btn_exportexcel()
    Dim dt = New DataTable()
    dt.Columns.Add("MTX PN")
    dt.Columns.Add("Inq#")
    dt.Columns.Add("Customer")
    dt.Columns.Add("Qty")
    dt.Columns.Add("Eng")
    dt.Columns.Add("A/M")
    For Each gvrow As GridViewRow In GridView1.Rows
        Dim chk As Boolean = DirectCast(gvrow.FindControl("cb_supship"), CheckBox).Checked
        If chk = True Then
            Dim i = gvrow.RowIndex
            Dim lbl_mtxpn As Label = gvrow.FindControl("lbl_mtxpn")
            Dim lbl_inqnum As Label = gvrow.FindControl("lbl_inqnum")
            Dim lbl_customer As Label = gvrow.FindControl("lbl_customer")
            Dim lbl_SamplesRequested As Label = gvrow.FindControl("lbl_SamplesRequested")
            Dim lbl_AssignedTo As Label = gvrow.FindControl("lbl_AssignedTo")
            Dim lbl_LTN_Eng As Label = gvrow.FindControl("lbl_LTN_Eng")
            Dim lbl_AcctMGR As Label = gvrow.FindControl("lbl_AcctMGR")

            Dim dr = dt.NewRow()
            dr.Item("MTX PN") = Convert.ToString(lbl_mtxpn.Text)
            dr.Item("Inq#") = Convert.ToString(lbl_inqnum.Text)
            dr.Item("Customer") = Convert.ToString(lbl_customer.Text)
            dr.Item("Qty") = Convert.ToString(lbl_SamplesRequested.Text)
            dr.Item("Eng") = Convert.ToString(lbl_LTN_Eng.Text) + "(" + Convert.ToString(lbl_AssignedTo.Text) + ")"
            dr.Item("A/M") = Convert.ToString(lbl_AcctMGR.Text)

            dt.Rows.Add(dr)
        End If
    Next
    Dim GridView2 = New GridView()
    GridView2.DataSource = dt
    GridView2.DataBind()
    Response.Clear()
    Response.Buffer = True
    Response.ContentType = "application/ms-excel"
    Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.xls", "selectedrows"))
    Response.Charset = ""
    Dim sw As New StringWriter()
    Dim hw As New HtmlTextWriter(sw)
    GridView2.RenderControl(hw)
    Response.Output.Write(sw.ToString())
    Response.End()
End Sub

As I said, without the export function the gridview.databind() works as expected and updates the gridview. As soon as the export function is put inbetween, it blocks the .databind() from happening.

Any ideas? Just for giggles I also tried a response.redirect instead and that has the same issue.


回答1:


This is happening because you are:

  1. Clearing the response;
  2. Sending the Excel file;
  3. Ending the response.

In other words, the server's reply to your request is to send the excel file. It doesn't send a new batch of HTML to the browser, because you told it to stop after the file was sent. As you have observed, your page doesn't change, because you didn't send your browser any new HTML.

I don't believe it's possible to both send a file AND send new HTML to the browser, but I'm open to being proved wrong. Most cases I've seen of people attempting both these things involve a rebind and page refresh first, and then an Ajax-like GET call to the server to get the Excel file. Other options include opening a new, very small window that just does a GET and returns the Excel file, and then closes after it's sent.




回答2:


Are you trying to have 2 GridViews or two views of the same data displayed in the same GridView?



来源:https://stackoverflow.com/questions/13571822/asp-gridview-refresh-after-excel-export-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!