I couldn't reproduce your problem, here is my testing code:<% @ Page Language="C#" %>
<% @ Import Namespace="System.IO"%>
<% @ Import Namespace="System.Data"%>
<% @ Import Namespace="System.Data.SqlClient"%>
<script Languagle="C#" runat="server">
public void Page_Load(Object src,EventArgs e)
{
string strconn ="server=localhost;uid=sa;pwd=;database=northwind";
string stracce ="Select * from Employees";
DataSet ds = new DataSet();
SqlDataAdapter Myadapter = new SqlDataAdapter (stracce, strconn);
Myadapter.Fill(ds,"Employees");
DataGrid1.DataSource=ds;
DataGrid1.DataMember ="Employees";
DataGrid1.DataBind(); Response.AddHeader("Content-Disposition", "attachment; filename=KenWang.xls");
Response.Charset = "UTF-8";
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState= false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End(); }
</script>
<html>
<head>
<title>sample</title>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server"
AutoGenerateColumns="true"
/>
</form>
</body>
</html>
<% @ Import Namespace="System.IO"%>
<% @ Import Namespace="System.Data"%>
<% @ Import Namespace="System.Data.SqlClient"%>
<script Languagle="C#" runat="server">
public void Page_Load(Object src,EventArgs e)
{
string strconn ="server=localhost;uid=sa;pwd=;database=northwind";
string stracce ="Select * from Employees";
DataSet ds = new DataSet();
SqlDataAdapter Myadapter = new SqlDataAdapter (stracce, strconn);
Myadapter.Fill(ds,"Employees");
DataGrid1.DataSource=ds;
DataGrid1.DataMember ="Employees";
DataGrid1.DataBind(); Response.AddHeader("Content-Disposition", "attachment; filename=KenWang.xls");
Response.Charset = "UTF-8";
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState= false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End(); }
</script>
<html>
<head>
<title>sample</title>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server"
AutoGenerateColumns="true"
/>
</form>
</body>
</html>
我有二个表没有任何关系,能导在一张sheet1表上吗?
<% @ Page Language="C#" %>
<% @ Import Namespace="System.IO"%>
<% @ Import Namespace="System.Data"%>
<% @ Import Namespace="System.Data.SqlClient"%>
<script Languagle="C#" runat="server">
public void Page_Load(Object src,EventArgs e)
{
string strconn ="server=localhost;uid=sa;pwd=;database=northwind";
string stracce ="Select * from Employees;select * from Categories";
DataSet ds = new DataSet(); SqlDataAdapter Myadapter = new SqlDataAdapter (stracce, strconn);
Myadapter.Fill(ds);
DataGrid1.DataSource=ds.Tables[0].DefaultView;
DataGrid1.DataBind();
DataGrid2.DataSource=ds.Tables[1].DefaultView;
DataGrid2.DataBind(); Response.AddHeader("Content-Disposition", "attachment; filename=KenWang.xls");
Response.Charset = "UTF-8";
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState= false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
DataGrid1.RenderControl(hw);
DataGrid2.RenderControl(hw);
Response.Write(tw.ToString());
Response.End(); }
</script>
<html>
<head>
<title>sample</title>
</head>
<body>
<form runat="server">
<asp:DataGrid id="DataGrid1" runat="server"
AutoGenerateColumns="true"
/>
<asp:DataGrid id="DataGrid2" runat="server"
AutoGenerateColumns="true"
/>
</form>
</body>
</html>
System.Web.HttpException: 类型“DataGridLinkButton”的控件“DataGrid1__ctl14__ctl1”必须放在具有 runat=server 的窗体标记内。楼主你是怎么解决的啊、
help?