5

I want to covnvert GridView data to Excel sheet.

I have written the code below, but it gives error:

protected void Button1_Click(object sender, EventArgs e) 
{       
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=Avukat.xls");
    Response.Charset = "";

    Response.ContentType = "application/vnd.xls";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    GridView1.RenderControl(htmlWrite);
    Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
    Response.Write(stringWrite.ToString());
    Response.End();
}

Error:

Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server.

0

5 Answers 5

2

I think your gridview contains a linkbutton/Imagebutton or another type of control, and that's why you are getting an Exception when you are trying to export the GridView to Excel.

Before using the control you need to add the following lines in your Page code behind, or the BasePage code behind.

public override void VerifyRenderingInServerForm(Control control)
{
}

You can use this code, as this code is tested and worked perfectly:

System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Response.AddHeader("content-disposition", "attachment; filename=Avukat.xls");
Response.ClearContent();

Response.AddHeader("content-disposition", attachment);

GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
Sign up to request clarification or add additional context in comments.

3 Comments

it shows error in this line Response.AddHeader("content-disposition", attachment);
" Control 'ctl00_ContentPlaceHolder1_GridView1' of type 'GridView' must be placed inside a form tag with runat=server " this error i got it.
@lucky jain; I have updated answer, you can try now, I am sure now your problem will resolved.
2

You've missed this part:

HtmlForm _HtmlForm= new HtmlForm();
GridView1.Parent.Controls.Add(_HtmlForm);
_HtmlForm.Attributes["runat"] = "server";
_HtmlForm.Controls.Add(GridView1);
_HtmlForm.RenderControl(htmlWrite);

I mean you should use this line of code:

_HtmlForm.RenderControl(htmlWrite);

Instead of:

GridView1.RenderControl(htmlWrite); 

But I think it would be better to use this free open source component which imports/exports from/to different excel and word formats: http://npoi.codeplex.com/

Comments

1

You just need to copy below code.

public override void VerifyRenderingInServerForm(Control control)
    {

        // Confirms that an HtmlForm control is rendered for the
        specified ASP.NET server control at run time.

    }

3 Comments

what is write inside that function i don't understand pl z suggest me
@lucky jain: do not write any code, just place this code on your .cs page. Hope it will help for you.
thanks ya i got it there is i have to write one statement in page header that is " EnableEventValidation="false" "
1
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

this.gvTask.RenderBeginTag(htmlWrite);
this.gvTask.HeaderRow.RenderControl(htmlWrite);
foreach (GridViewRow row in this.gvTask.Rows)
{
    row.RenderControl(htmlWrite);
}
this.gvTask.FooterRow.RenderControl(htmlWrite);
this.gvTask.RenderEndTag(htmlWrite); 

Comments

0
//**how gridview data download in excel in asp.net**

protected void btnDownload_Click(object sender, EventArgs e)
{
    CommonFunction objComm = new CommonFunction();
    Hashtable objHash = new Hashtable();
    //Hashtable htParam = new Hashtable();
    objHash.Clear();
    string str = ddlMonthlyYrs.SelectedItem.Text.ToString();
    if (str == "Select")
    {
        objHash.Add("@Cmonth", "");
    }
    else
    {
        objHash.Add("@Cmonth", str.Substring(0, 6));
    }
    if (ddlPaymentTerm.SelectedItem.Text == "Select")
    {
        objHash.Add("@PaymentTerm", "");
    }
    else
    {
        objHash.Add("@PaymentTerm", ddlPaymentTerm.SelectedValue.ToString());
    }
    if (ddlPayMode.SelectedItem.Text == "Select")
    {
        objHash.Add("@PaymentMode", "");
    }
    else
    {
        objHash.Add("@PaymentMode", ddlPayMode.SelectedValue);
    }

    objHash.Add("@PolicyNo", txtPolicyNo.Text);

    objHash.Add("@AgentCode", txtAgnCode.Text);
    objHash.Add("@AgentName", txtAgnName.Text);

    DataSet objDS = objComm.GetDataSetForPrcDBConn("Prc_GetIncBasedataRp", objHash, "Commssion");

    BasicComm.CommonFunction objCom = new BasicComm.CommonFunction();
    Response.Clear();
    //Response.Charset=”";
    Response.ContentType = "application/vnd.ms-excel";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
    System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
    dg.DataSource = objDS.Tables[0];
    dg.DataBind();
    dg.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.End();
}

1 Comment

Hi pradeep, and welcome to Stackoverflow. However, please consider formatting the code correctly. You can use the "code" style in the editor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.