用户代码未处理HttpException
类型“GridView”的控件"toexcelto"必须放在具有runat=server的窗体标记内。我的前台代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="toexcel1.aspx.cs" Inherits="toexcel1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www. >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" method="post" runat="server">
<div runat="server">
<table style="width: 377px; height: 123px" runat="server">
<tr>
<td>
</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<asp:GridView ID="toexcelto" runat="server" BackColor="White" BorderColor="#3366CC"
BorderStyle="None" BorderWidth="1px" CellPadding="4">
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="商品号" />
<asp:BoundField DataField="ProductName" HeaderText="商品名称" />
<asp:BoundField DataField="UnitPrice" HeaderText="商品单价" />
</Columns>
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></td>
</tr>
</table>
</div>
</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.Drawing;
using
using System.Text;
using System.Data.SqlClient;
public partial class toexcel1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bandexcel();
}
}
private void bandexcel()
{
SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
conn1.Open();
string cmdtext = "select top 5 * from products";
SqlDataAdapter sa = new SqlDataAdapter(cmdtext,conn1);
DataSet ds = new DataSet();
sa.Fill(ds,"excel");
toexcelto.DataSource=ds.Tables["excel"].DefaultView;
toexcelto.DataBind();
conn1.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "gb2312";
EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
toexcelto.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
}
请问各位大哥们我该怎么处理这个问题啊 烦死了 书上的案例运行的时候正常 为什么我的就不能呢 跪求啊