2016-07-09 64 views
0

我有一個下拉稱爲company和一個按鈕叫export to excel和一個gridview,我也有兩個控件的更新面板,但是當我導出到Excel下拉列表將不會就算了工作,一要導出2倍我不能沒有提神這一頁。後我出口的GridView控件excel文件我的下拉列表將無法正常工作?

我出口代碼:

protected void Button1_Click(object sender, EventArgs e) 
    { 

     // HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.AddHeader(
      "content-disposition", string.Format("attachment; filename={0}", "Kontratat-" + DateTime.Now.ToShortDateString() + ".xls")); 
     HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; 


     StringWriter sw = new StringWriter(); 
     HtmlTextWriter hw = new HtmlTextWriter(sw); 


     HtmlForm frm = new HtmlForm(); 
     hw.WriteLine("<center><b><u><font size='5'>Kontratat</font></u></b></center>");//will be displayed in excel as a heading. 
     GridView1.Parent.Controls.Add(frm); 
     frm.Controls.Add(GridView1); 
     frm.RenderControl(hw); 
     HttpContext.Current.Response.Write(sw.ToString()); 
     HttpContext.Current.Response.Flush(); 
     HttpContext.Current.Response.End(); 

    }    

和我的aspx代碼

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
<Triggers> 
    <asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" /> 
    <asp:PostBackTrigger ControlID="Button1" /> 
</Triggers> 
<ContentTemplate> 
    <asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" CellSpacing="2" ForeColor="Black"> 
    </asp:GridView> 
</ContentTemplate> 

幫我請

+0

你爲什麼這個標籤作爲ASP.NET MVC? – mason

回答

0

我覺得在代碼返回的XLS文件應呈現更新面板的內容不是一個好的解決方案。你爲什麼不重定向到包含您的按鈕的代碼單擊作爲Page_Load中新的一頁?這樣一個全新的頁面將生成的文件時,瀏覽器會詢問保存它,它會自動然後重定向到你的本金頁面。你可以試試這種方式嗎?

相關問題