类型“Button”的控件“replist_Button2_0”必须放在具有 runat=server 的窗体标记内。Repeater导出数据出错
protected void BtnPrint_Click(object sender, EventArgs e)
{
//定义文档类型、字符编码
string title = "**统计表" + "_" + DateTime.Today.ToShortDateString();
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
string filename = "attachment;filename=" + System.Web.HttpUtility.UrlEncode(title, System.Text.Encoding.UTF8) + ".xls";
Response.AppendHeader("Content-Disposition", filename);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
Response.ContentType = "application/ms-excel";
Response.Write("<meta http-equiv=Content-Type content="text/html; charset=GB2312">");
this.EnableViewState =true;
// 定义一个输入流
System.IO.StringWriter os = new System.IO.StringWriter();
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.replist.RenderControl(oHtmlTextWriter);
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
Response.Write(oStringWriter.ToString());
Response.End();
}
前台:
<table id="table1" >
<tr>
<th>
  活动名
</th>
<th >
  活动封面
</th>
<th>
  组织者
</th>
<th>
  目的地
</th>
<th>
  活动结束时间
</th>
<th>
  操作
</th>
</tr>
<asp:Repeater ID="replist" runat="server" OnItemCommand="replist_ItemCommand">
<ItemTemplate>
<tr>
<td>
<%# Eval("name")%>
</td>
<td >
<img style=" width:200px; height:150px;" src="//img.reader8.net/uploadfile/logo/<%#Eval("logo") %>" />
</td>
<td>
<%# Eval("zhuzhizhe")%>
<asp:Label ID="lblid" runat="server" Text='<%# Eval("lid")%>' Visible="False"></asp:Label>
</td>
<td>
<%# Eval("didian")%>
</td>
<td>
<%# Eval("edds").ToString().Split(' ')[0].ToString()%>
</td>
<td >
<a href="Addhd.aspx?lid=<%# Eval("lid")%>" id="yy" target="list">更改</a>
<%-- <asp:LinkButton ID="lid" Text="预约" OnClick="yy_Click" runat="server"></asp:LinkButton>--%>
<asp:Button class="btn1" ID="Button2" runat="server" Text="删除" CommandName="del" CommandArgument='<%# Eval("lid") %>'
OnClientClick="return confirm('确定删除吗?');" />
<asp:Button ID="dc" runat="server" OnClick="BtnPrint_Click" Text="导出" class="btn1"/>
</td>
</tr>
</ItemTemplate>
</asp:Repeater> ASP 服务器控件
[解决办法]
在页面中重写Page基类的VerifyRenderingInServerForm方法
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
参考http://www.cnblogs.com/zhangronghua/archive/2007/11/07/951899.html