我在Ajax UpdatePanel中有一个Gridview。在每个GV行中,我有一个复选框字段。想法是用户检查他们想要的行,然后单击一个按钮将该行中的标签更新为“已发货”,然后将检查的行导出到xls文件(真的是csv)。
当我的代码隐藏触发它遍历gridview行时,查找检查,更新数据库以标记每一行,然后我使用.DataBind()刷新网格。这完全符合预期。
现在我还想将选中的行导出到excel。所以我创建了一个执行此操作的方法,并在更新每一行以标记行之后和在.DataBind()刷新之前弹出它。现在.DataBind()永远不会触发刷新。我收到了XLS下载,如果我手动刷新屏幕,则会按预期更新这些行。
这是我的代码:
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()发生。
有什么想法吗?只是为了咯咯笑,我还尝试了一个response.redirect,它有同样的问题。
答案 0 :(得分:1)
这种情况正在发生,因为你是:
换句话说,服务器对您的请求的回复是发送excel文件。它不会向浏览器发送新批HTML,因为您告诉它在文件发送后停止。如您所见,您的页面不会更改,因为您没有向浏览器发送任何新的HTML。
我不相信发送文件和向浏览器发送新的HTML都是可能的,但我很容易被证明是错的。我见过的人们尝试这两件事的大多数情况首先涉及重新绑定和页面刷新,然后是一个类似于Ajax的GET调用服务器来获取Excel文件。其他选项包括打开一个新的非常小的窗口,只需执行GET并返回Excel文件,然后在发送后关闭。
答案 1 :(得分:0)
您是否尝试在同一GridView中显示2个GridView或两个相同数据的视图?