做DATAGRID导出EXCEL文件时,遇到的问题...

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

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

源错误:


行 49: Dim hw As New System.Web.UI.HtmlTextWriter(tw)
行 50: ' 获取control的HTML
行 51: MyDataGrid.RenderControl(hw)
行 52: ' 把HTML写回浏览器
行 53: Response.Write(tw.ToString())


源文件: c:\inetpub\wwwroot\vehicleMIS\vehicle_query.aspx.vb 行: 51



源文件是这样的:
Response.ContentType = "application/vnd.ms-excel"
' 从Content-Type header中去除charset设置
Response.Charset = ""

' 关闭 ViewState
Me.EnableViewState = False
Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)
' 获取control的HTML
MyDataGrid.RenderControl(hw)
' 把HTML写回浏览器
Response.Write(tw.ToString())
Response.End()

这些语句我并不是很懂,请高手指点,这是什么问题?
...全文
57 6 打赏 收藏 举报
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdnshao 2003-07-26
  • 打赏
  • 举报
回复
不要允许排序
saucer 2003-07-21
  • 打赏
  • 举报
回复
in your case, you need to modify the regular expressions to remove the stuffs before and after the datagrid, for example

s = System.Text.RegularExpressions.Regex.Replace(s,"^.*?(<table[^>]+id=""MyDataGrid"".*?</table>).*$","$1",System.Text.RegularExpressions.RegexOptions.IgnoreCase or System.Text.RegularExpressions.RegexOptions.Singleline)
saucer 2003-07-21
  • 打赏
  • 举报
回复
you are right, you might need to remove those link buttons from the datagrid, see a solution by phuff
http://www.asp.net/Forums/ShowPost.aspx?tabindex=1&PostID=184315

or try my method,
<form id="form1" runat=server>

instead of calling DataGrid.RenderControl, call form1.RenderControl, then remove the string before and after the datagrid table

form1.RenderControl(hw)
dim s = tw.ToString()
s = System.Text.RegularExpressions.Regex.Replace(s,"^.*?(<table.*?</table>).*$","$1",System.Text.RegularExpressions.RegexOptions.IgnoreCase or System.Text.RegularExpressions.RegexOptions.Singleline)

Response.Write(s)
baowang 2003-07-20
  • 打赏
  • 举报
回复
up
baowang 2003-07-20
  • 打赏
  • 举报
回复
我的源文件是这样的,好像没什么问题呀?

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="vehicle_query.aspx.vb" Inherits="vehicleMIS.query"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>query</title>
<meta content="Microsoft Visual Studio .NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" runat="server">
<table align="center">
<TR>
<TD style="HEIGHT: 17px"><FONT face="宋体">车牌号码
<asp:DropDownList id="DropDownList_brand_number" runat="server"></asp:DropDownList>
<asp:Button id="button_query" runat="server" Text="查询"></asp:Button></FONT></TD>
</TR>
<TR>
<TD><FONT face="宋体"></FONT></TD>
</TR>
<tr>
<td>
<asp:datagrid id="MyDataGrid" runat="server" AllowPaging="True" PagerStyle-Mode="NumericPages" PagerStyle-HorizontalAlign="Right" OnPageIndexChanged="MyDataGrid_Page" BorderColor="black" BorderWidth="1" GridLines="Both" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" AlternatingItemStyle-BackColor="#eeeeee">
<AlternatingItemStyle BackColor="#EEEEEE"></AlternatingItemStyle>
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
<PagerStyle HorizontalAlign="Right" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
<p style="FONT-SIZE: 9pt"><asp:label id="lblPageCount" runat="server"></asp:label> 
<asp:label id="lblCurrentIndex" runat="server"></asp:label><asp:linkbutton id="btnFirst" onclick="PagerButtonClick" runat="server" Font-Name="verdana" CommandArgument="0" ForeColor="navy" Font-size="8pt"></asp:linkbutton> 
<asp:linkbutton id="btnPrev" onclick="PagerButtonClick" runat="server" Font-Name="verdana" CommandArgument="prev" ForeColor="navy" Font-size="8pt"></asp:linkbutton> 
<asp:linkbutton id="btnNext" onclick="PagerButtonClick" runat="server" Font-Name="verdana" CommandArgument="next" ForeColor="navy" Font-size="8pt"></asp:linkbutton> 
<asp:linkbutton id="btnLast" onclick="PagerButtonClick" runat="server" Font-Name="verdana" CommandArgument="last" ForeColor="navy" Font-size="8pt"></asp:linkbutton></p>
</td>
</tr>
</table>
</form>
</body>
</HTML>
saucer 2003-07-20
  • 打赏
  • 举报
回复
add your datagrid between
<form runat=server>
<asp:datagrid id="MyDataGrid" runat="server" />
</form>

see
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q317719
相关推荐
发帖
.NET社区

6.1w+

社区成员

.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
帖子事件
创建了帖子
2003-07-20 12:57
社区公告

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

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