后台代码:
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("名称", System.Text.Encoding.UTF8) + ".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); // repeater控件的ID
rpt_storeinfo.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
前台代码:
<asp:Repeater ID="rpt_storeinfo" runat="server" OnItemDataBound="rpt_storeinfo_ItemDataBound" >
<ItemTemplate>
<tr id="tr_info" runat="server" style="height: 22px">
<td style='background-color: white;' align="right">
<%# Eval("Belong").ToString().ToUpper()%><br><%#Eval("BelongN").ToString()%>
</td>
<td align="center">
<asp:Label ID="lbl_date" runat="server" Text='<%# Eval("riqi","{0:d}") %>' />
</td>
<td style="display: none;">
<asp:HiddenField ID="hidden_Belong" runat="server" Value='<%# Eval("Belong") %>' />
<asp:HiddenField ID="hidden_amount" runat="server" Value='<%# Eval("amount") %>' />
<asp:Label ID="lbl_amount" runat="server" Text='<%# Eval("amount").ToString()=="0"?"<span style=color:red;font-size:12px>无库存纪录</span>":"库存:"+Eval("amount").ToString()+" - " %>' />
<asp:Label ForeColor="Blue" ID="lbl_PDtime" runat="server" Text='<%# Eval("pdTime","{0:d}") %>' />
</td>
<td>
<asp:Label ID="lbl_ID" runat="server" Text='<%# Eval("bianhao") %>' />
</td>
<td align="right">
<asp:Label ID="lbl_WL" runat="server" Text='<%# Eval("WLbianhao") %>'/><br>
<asp:Label ID="lbl_PM" runat="server" Text='<%# Eval("pinmingguige") %>'/>
</td>
<td align="right">
<asp:Label ID="lbl_LaiQty" runat="server" Text='<%# Eval("LaiQty") %>' />
</td>
<td align="right">
<asp:Label ID="lbl_OutQty" runat="server" Text='<%# Eval("OutQty") %>' />
</td>
<td style="font-size: 14px; font-weight: bold;" align="right">
<asp:Label ID="lbl_jieyu" runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td colspan="9">
<%# ((System.Data.DataTable)ViewState["store"]).Rows.Count == 0 ? "<font color=red>无记录</font>":""%>
</td>
</tr>
</FooterTemplate>
</asp:Repeater>
遇到的问题是:HiddenField”的控件“ctl00_ctpMaster_rpt_storeinfo_ctl00_hidden_Belong”必须放在具有 runat=server 的窗体标记内
在页面中重写Page基类的VerifyRenderingInServerForm方法(加在后台代码中)
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
且:<form id="form1" runat="server">