0
我有一個用戶控件定義如下。使用UpdatePanel從UserControl下載文件
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table>
<tr>
<td>
<asp:ImageButton ID="ImageButton" runat="server" />
</td>
<td>
<asp:Label ID="lblHeader" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
<asp:Button runat="server" ID="saveLayoutBtn" Text="Save" OnClick="SaveBtn_Click"
ToolTip="Save current grid layout" />
<asp:Button ID="Home_ExportExcel" runat="server" Text="Export To Excel" OnClick="Home_ExportExcel_Click" Visible="false" />
<asp:PlaceHolder ID="placeHolder" runat="server"></asp:PlaceHolder>
<asp:Label ID="notificationLbl" runat="server" Text="" Font-Bold="true"></asp:Label>
<div id="DataGridWrapper">
<!--A Grid View tat displays Data-->
</div>
</ContentTemplate>
</asp:UpdatePanel>
現在,被點擊的Home_ExportExcel
的時候,我想GridDtata要導出爲Excel/xml文件。 我在usercontrol.ascx.cs
protected void Home_ExportExcel_Click(object sender, EventArgs args)
{
GenerateWorkSheetWithSB(resultTbl, this.MasterPage.CurrentLibrary);
}
public void GenerateWorkSheetWithSB(DataTable resultRequests, string libName, string advancedSearch = null)
{
WindowsImpersonationContext impersonationContext;
if (BLSecurity.ImpersonateFacelessAccount(out impersonationContext))
{
int iWorkSheet = 1, iCol = 1, iRow = 1;
System.Text.StringBuilder strExcelXml = new System.Text.StringBuilder();
try
{
iWorkSheet = 1;
iCol = 15;
iRow = resultRequests.Rows.Count + 1;
iWorkSheet++;
iRow++;
iCol++;
}
catch (Exception ex)
{
}
//First Write the Excel Header
strExcelXml.Append(ExcelHeader());
// Get all the Styles
strExcelXml.Append(ExcelStyles());
// Worksheet options Required only one time
strExcelXml.Append(ExcelWorkSheetOptions());
for (int i = 1; i < iWorkSheet; i++)
{
// Create First Worksheet tag
strExcelXml.Append("<Worksheet ss:Name=\"WorkSheet" + i.ToString() + "\">");
// Then Table Tag
strExcelXml.Append("<Table ss:DefaultColumnWidth=\"150\" >");
SetupExcelHeaderWorksheet(strExcelXml, libName);
foreach (DataRow row in resultRequests.Rows)
//foreach (DataRow row in resultRequests.Rows)
SetupExcelDataWorksheet(strExcelXml, row, libName);
strExcelXml.Append("</Table>");
strExcelXml.Append("</Worksheet>");
}
// Close the Workbook tag (in Excel header you can see the Workbook tag)
strExcelXml.Append("</Workbook>\n");
#region "Write Into File"
string uploadTempPath = "D:"; //ProfileBroker.GetProfileValue(AppProfileConst.UPLOAD_SERVER_TEMPPATH);
string tmpFilePath = uploadTempPath + "\\HomeGridData.xml";
string convertData = ConvertHTMLToExcelXML(strExcelXml.ToString());
byte[] byteArray = Encoding.UTF8.GetBytes(convertData);
//MemoryStream stream = new MemoryStream(byteArray);
File.WriteAllBytes(tmpFilePath, byteArray);
//this.Page.Response.Buffer = true;
this.Page.Response.Clear();
this.Page.Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFileName(tmpFilePath));
this.Page.Response.ContentType = "application/octet-stream";
this.Page.Response.TransmitFile(tmpFilePath); //does not work inside WARP unless there is JS, refer to ascx file
this.Page.Response.End();
#endregion
SecurityUtils.UndoImpersonation(impersonationContext);
}
else
{
Response.Write("<script>alert('File does not exist. Please contact tool support.')</script>");
}
}
我沒有得到任何文件下載下面的代碼,而不是我得到的網格數據作爲http響應XML格式。 如果我在.aspx頁面上使用相同的代碼,它工作正常,文件被下載。 我在SO上發現了this和this,發現使用UpdatePanel可能是個問題。但我不確定我在這裏錯過了什麼。
Ok..But回發發生,現在當我們點擊作爲我收到response..How導出按鈕將文件從這個不同那post ack – Programmerzzz
默認情況下,更新面板實現異步回發,所以如果我們想要一個完整的帖子回來,我們需要明確地創建一個回帖觸發器 – Krishna
謝謝,'克里希納',這對我有效。 – Programmerzzz