求助高手DataGrid导为Excel的问题

zzy1254 2006-10-26 08:48:40
我的程序总是出现莫名的状况:
错误说明:

“/BillPrint”应用程序中的服务器错误。
--------------------------------------------------------------------------------

类型“DataGridLinkButton”的控件“DataGrid1__ctl1__ctl1”必须放在具有 runat=server 的窗体标记内。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.Web.HttpException: 类型“DataGridLinkButton”的控件“DataGrid1__ctl1__ctl1”必须放在具有 runat=server 的窗体标记内。

源错误:


行 72: System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
行 73: System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
行 74: this.DataGrid1.RenderControl(oHtmlTextWriter);
行 75: Response.Write(oStringWriter.ToString());
行 76: Response.End();


源文件: e:\inetpub\wwwroot\billprint\webform4.aspx.cs 行: 74



可是我确实把DataGrid放在里面了啊,如下(网页源码):

<%@ Page language="c#" Codebehind="WebForm4.aspx.cs" AutoEventWireup="false" Inherits="BillPrint.WebForm4" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm4</title>
<meta name="vs_snapToGrid" content="False">
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 9px; POSITION: absolute; TOP: 38px" runat="server"
Width="944px" AllowPaging="True" PageSize="20" CellPadding="4" BackColor="White" BorderWidth="1px"
BorderStyle="None" BorderColor="#3366CC">
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" Position="Top" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:Button id="Button2" style="Z-INDEX: 107; LEFT: 145px; POSITION: absolute; TOP: 10px" runat="server"
Width="109" Height="24" Text="生成Excel表"></asp:Button>
<asp:Button id="Button1" style="Z-INDEX: 106; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
Width="109px" Height="24px" Text="数据预览"></asp:Button></FONT>
</form>
</body>
</HTML>


请各位高手帮帮忙吧,这是上班的第一个程序,会要人命的啊!!!!!!!!!!
...全文
179 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
greenberet007 2006-10-26
  • 打赏
  • 举报
回复

我刚才说的哪个方法如果有分页情况是不能导成Excel的
刚才忘记说了 :-)
daishengs 2006-10-26
  • 打赏
  • 举报
回复
AllowPaging="False"

在有分页的情况下,是不能导成EXCEL的,就会出现什么一定要放在什么runat=server 的窗体标记内.
还有,如果DataGrid中定义了模板列,而且模板列里面有ASP.NET控件,也会出现什么runat=server 的窗体标记内
greenberet007 2006-10-26
  • 打赏
  • 举报
回复
试试这段代码

protected void btnPrint_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//设置输出流为简体中文
Response.ContentType = "application/ms-excel";
//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid控件的名字.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
zzy1254 2006-10-26
  • 打赏
  • 举报
回复
我已经移过两个窗体了,还是不行,昨天还没问题 的
xingyaohua 2006-10-26
  • 打赏
  • 举报
回复
没问题,
你重建一个窗体,把所有控件和代码移过去试试

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧