2010-11-28 28 views
3

大家好我每個人使用c#.net來使web應用程序。 我正在製作一個將gridview數據導出爲ex​​cel的網頁。我的代碼遵循問題可見虛假的按鈕時,將數據導出到excel

protected void btnexport_Click(object sender, EventArgs e) 
    { 
     btnsubmit.Visible = false; 
     exporttoexcel(); 
     expToExcel(); 
    } 

    public override void VerifyRenderingInServerForm(Control control) 
    { 
     //base.VerifyRenderingInServerForm(control); 
    } 

    public void exporttoexcel() 
    { 
     Context.Response.Clear(); 

     Context.Response.AddHeader("content-disposition", "attachment;filename=FileName.xls"); 

     Context.Response.Charset = ""; 

     Context.Response.Cache.SetCacheability(HttpCacheability.NoCache); 

     Context.Response.ContentType = "application/vnd.xls"; 

     System.IO.StringWriter stringWrite = new System.IO.StringWriter(); 

     System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); 

     GridView1.RenderControl(htmlWrite); 

     Context.Response.Write(stringWrite.ToString()); 

     Context.Response.End(); 

    } 

我的GridView的數據導出成功,但是,EXPORTTOEXCEL按鈕單擊事件我試圖另一個按鈕的假的Visible屬性提交出類拔萃,但我成功做到這一點,所以請告訴我我怎麼可見屬性設置爲false EXPORTTOEXCEL按鈕單擊事件

請給我回信很快我的工作是在中途

感謝ü所有提前

+0

我猜btnexport.Visible = FALSE; – 2010-11-28 09:29:31

回答

1

您設置的按鈕知名度回發時爲false,但在相同的請求中,您清除響應並輸出Excel數據。所以帶有不可見按鈕的控制樹永遠不會將其返回給客戶端。

我猜你想要這樣做,以便在生成Excel時隱藏用戶的按鈕。如果是這種情況,最好的辦法是使用客戶端JavaScript來隱藏按鈕。

你可以在按鈕的OnClientClick屬性插入此javascript:

document.getElementById('<%=btnexport.ClientID%>').style.display = none; 
+0

謝謝你的回答,但它不工作仍然我無法設置該屬性可見虛假的按鈕,請給我另一種解決方案,如果你有任何其他代碼導出的GridView數據爲Excel請給我 – 2010-11-29 16:08:40