how do I download only my gridview in c #?

0

I have a problem when downloading my GridView to excel since my method causes the whole page to be downloaded as for example the DropDownList that it contains, before I had the code in a Template but I passed it to a Form and it continues to mark the same error

  

{"The 'grvTotal1' control of type 'GridView' must be placed within   a form tag with runat = server. "}

that is the code that this using

protected void ImBExcel_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            HttpResponse response = Response;
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);

            string FileName = "PC" + DateTime.Now + ".xls";

            response.Clear();
            response.Buffer = true;
            response.ClearContent();
            response.ClearHeaders();
            response.Charset = "";

            response.Cache.SetCacheability(HttpCacheability.NoCache);
            response.ContentType = "application/vnd.ms-excel";
            response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);

            response.Charset = "UTF-8";
            response.ContentEncoding = System.Text.Encoding.Default;

            CargarTotales();

            grvTotal1.RenderControl(htw);
            response.Write(sw.ToString());
            response.End();
        }
        catch (HttpException exp)
        {

        }

    }
    
asked by Joan 28.08.2018 в 20:44
source

0 answers