<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" Height="66px" OnClick="Button1_Click" Text="Button"
Width="72px" />
</form>
</body>
</html>using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Server=localhost;Database=pubs;user id=sa;password=sa"; SqlConnection myConnection = new SqlConnection(connectionString); SqlDataAdapter ad = new SqlDataAdapter("select * From authors ", myConnection); DataSet ds = new DataSet(); ad.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GridView1.RenderControl(htw);
string strHtml = sw.ToString().Trim();
string ExcelFileName = "FoodList.xls";
string FilePhysicialPathName = Request.PhysicalApplicationPath;
//生成的Excel文件名
string objectExcelFileName = Path.Combine(FilePhysicialPathName, ExcelFileName);
if (File.Exists(objectExcelFileName))
{
File.Delete(objectExcelFileName);
}
FileStream fs = new FileStream(objectExcelFileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs, Encoding.GetEncoding("GB18030"));
bw.Write(strHtml);
bw.Close();
fs.Close(); //类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
}
}按 Button1 按钮后
出错信息为: 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。 如何解决?
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" Height="66px" OnClick="Button1_Click" Text="Button"
Width="72px" />
</form>
</body>
</html>using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Server=localhost;Database=pubs;user id=sa;password=sa"; SqlConnection myConnection = new SqlConnection(connectionString); SqlDataAdapter ad = new SqlDataAdapter("select * From authors ", myConnection); DataSet ds = new DataSet(); ad.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GridView1.RenderControl(htw);
string strHtml = sw.ToString().Trim();
string ExcelFileName = "FoodList.xls";
string FilePhysicialPathName = Request.PhysicalApplicationPath;
//生成的Excel文件名
string objectExcelFileName = Path.Combine(FilePhysicialPathName, ExcelFileName);
if (File.Exists(objectExcelFileName))
{
File.Delete(objectExcelFileName);
}
FileStream fs = new FileStream(objectExcelFileName, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs, Encoding.GetEncoding("GB18030"));
bw.Write(strHtml);
bw.Close();
fs.Close(); //类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
}
}按 Button1 按钮后
出错信息为: 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。 如何解决?
<form id="form1" runat="server">
public override void VerifyRenderingInServerForm(Control control)
{ }