Harsh Jani

Harsh Jani

  • NA
  • 34
  • 53.7k

Export the contents of a Webform to a Text File

Mar 19 2015 3:24 AM
Hi,
 
I want to export the contents of a Web From to a Text file. I have three text boxes and a Grid View. I want to export these contents to a text file in a proper formatted manner. I have used the following code for the purpose: -
 
protected void Button1_Click(object sender, EventArgs e)
{
Response.AddHeader("content-disposition", "attachment;filename=Employee.txt");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/text";
StringWriter SW = new StringWriter();
HtmlTextWriter txtWriter = new HtmlTextWriter(SW);
HtmlForm frm = new HtmlForm();
GridView1.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(txtUName);
frm.Controls.Add(txtEmail);
frm.Controls.Add(txtCity);
frm.Controls.Add(GridView1);
frm.RenderControl(txtWriter);
Response.Write(SW.ToString());
Response.End();
}
 
This code is working fine but is returning data in HTML format. So, kindly help me out in this regard.