简体   繁体   English

将数据导出到Excel时,问题显示为按钮的错误

[英]problem to visible false of button when export data to excel

Hello Everybody I am using c#.net to make webapplication. 大家好我正在使用c#.net来制作web应用程序。 I am making a webpage that export gridview data to excel. 我正在制作一个将gridview数据导出为ex​​cel的网页。 My code is as follow 我的代码如下

    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();

    }

My gridview data is export to excel successfully but on EXPORTTOEXCEL button click event i am trying to false the visible property of another button submit, but i am successful to do it so please tell me how i set the visible property to false on EXPORTTOEXCEL button click event 我的gridview数据导出到excel成功,但在EXPORTTOEXCEL按钮单击事件我试图虚假的另一个按钮提交的可见属性,但我成功这样做所以请告诉我如何在EXPORTTOEXCEL按钮单击时将visible属性设置为false事件

Please give me reply soon my work is in midway 请尽快回复我的工作

thank u to all in advance 我要提前感谢你们

You are setting the button visibility to false on postback, but in the same request you clear the response and output your Excel data. 您在回发时将按钮可见性设置为false,但在同一请求中,您将清除响应并输出Excel数据。 So the control tree with the invisible button never makes it back to the client. 所以带有隐形按钮的控制树永远不会回到客户端。

I am guessing you want to do this in order to hide the button from the user while you are generating the Excel. 我猜你想要这样做是为了在生成Excel时隐藏用户的按钮。 If this is the case, your best bet is to use client-side javascript to hide the button. 如果是这种情况,最好的办法是使用客户端javascript来隐藏按钮。

You could insert this javascript in the button's OnClientClick property: 您可以在按钮的OnClientClick属性中插入此javascript:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM