在ASP导出不工作后ASP ASP GridView刷新 [英] ASP GridView Refresh after Excel Export not working

查看:142
本文介绍了在ASP导出不工作后ASP ASP GridView刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Ajax UpdatePanel中有一个Gridview。在每个GV行内,我都有一个复选框字段。这个想法是,用户检查他们想要的行,然后单击一个按钮,将该行中的标签更新为已发货,然后将已检查的行导出到xls文件(真正的csv)。

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).

当我的代码隐藏触发它遍历GridView行时,查找检查,更新数据库以标记每行,然后使用.DataBind()刷新网格。这完美的和预期的一样。

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.

现在我还想将选中的行导出到excel。所以我创建了一个方法来做到这一点,并在更新每行以标记行并在.DataBind()刷新之前将其弹出。现在,.DataBind()永远不会刷新。我确实收到了XLS下载,如果我手动刷新屏幕,则会按预期更新这些行。

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.

这里是我的代码:

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

正如我所说的,没有导出函数,gridview.databind()按预期工作并更新gridview。只要将导出功能置于其间,它就会阻止发生.databind()。

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.

任何想法?只是为了咯咯地笑,我也尝试了一个response.redirect来代替,并且它有相同的问题。

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

推荐答案

这是因为您是:
$ b

This is happening because you are:


  1. 清除响应;
  2. 发送Excel文件;
  3. 结束响应。

换句话说,服务器对您请求的回复是发送excel文件。它不会向浏览器发送一批新的HTML,因为您告诉它在发送文件后停止。正如您所观察到的,您的页面不会更改,因为您没有向浏览器发送任何新的HTML。

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.

我不相信可以发送文件并将新的HTML发送到浏览器,但我很乐意被证明是错误的。我见过的大多数人尝试这两种方式都涉及到重新绑定和页面刷新,然后对服务器进行类似Ajax的GET调用以获取Excel文件。其他选项包括打开一个新的非常小的窗口,只是进行GET并返回Excel文件,然后在发送后关闭。

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.

这篇关于在ASP导出不工作后ASP ASP GridView刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆