我在Ajax UpdatePanel中有一個Gridview。在每個GV行內,我都有一個複選框字段。這個想法是,用戶檢查他們想要的行,然後單擊一個按鈕,將該行中的標籤更新爲「已發貨」,然後將已檢查的行導出爲xls文件(真正的csv)。ASP GridView在Excel導出不工作後刷新
當我的代碼隱藏觸發它遍歷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而不是這個問題。
因此,似乎在我的btn_exportexcel子事件的「響應」部分的某個地方正在打破。這幾乎就像GridView1.DataBind()試圖對我爲電子表格創建的gridview一樣。如果這是有道理的... – dlevasseur
好吧。我將Response.End更改爲Response.Flush()Response.Close(),現在調試器將完成這些步驟並退回到原始方法並進入GridView1.Databind(),但屏幕仍然不會更改。我也有一個調試器從不涉足的Gridview1的onrowdatababound方法。所以基本上調試器會通過導出過程返回到按鈕單擊事件步驟到GridView1.DataBind(),然後立即到END SUB。 Hrm ... – dlevasseur