bug when I export the contents of a GridView control to an Excel f

G

Guest

I experienced a bug, "Control GridView1 of type GridView myst be placed
inside a form tag with runat=server". But I did so. I do not why, please
advise. my code as below. Thanks.
Peter


<%@ Page language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
// Response.Write("");
TheLabel.Text = " ";
}

void doExport(object sender, EventArgs e) {

string connectionString
= "server=myServer;database=myDatabase;uid=JohnDoe;pwd=xyz";
System.Data.SqlClient.SqlConnection con
= new System.Data.SqlClient.SqlConnection(connectionString);
using (con)
{
con.Open();
TheLabel.Text = "<b>table tblGeoMA</b> ";
string sql = "select * from tblGeoMA";
System.Data.SqlClient.SqlDataAdapter da
= new System.Data.SqlClient.SqlDataAdapter(sql, con);

System.Data.DataSet ds = new System.Data.DataSet();
da.Fill(ds,"dsTable1");

GridView1.DataSource = ds;
GridView1.DataMember = "dsTable1";


TheLabel.Text = " export the data in GridView to excel ";
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=peter1.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite
= new System.Web.UI.HtmlTextWriter(stringWrite);
GridView1.DataBind();
GridView1.RenderControl(htmlWrite);
//Response.Write(stringWrite.ToString());
Response.Write("hello");
Response.End();
}
}
</script>




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>GridView2Excel - test 5</title>
</head>
<body>
<form id="form1" runat="server">
<asp:button runat="server" text="export to excel" onclick="doExport"
/><br />
<asp:label runat="server" text="" id="TheLabel" />
<asp:GridView runat="server" id="GridView1" />
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top