类型“TextBox”的控件“tbMsg”必须放在具有 runat=server 的窗体标记内.
我想把TEXTBOX里面的内容输入到一个WORD文档中
private void BtnSave_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.Buffer= true;
Response.Charset= "utf-8 ";
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
//filename=msg.doc 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc || .xls || .txt ||.htm
Response.AppendHeader( "Content-Disposition ", "attachment;filename=msg.doc ");
Response.ContentEncoding=System.Text.Encoding.GetEncoding( "utf-8 ");
//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档
Response.ContentType = "application/ms-word ";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
tbMsg.RenderControl(oHtmlTextWriter);
//你也可以绑定其他支持obj.RenderControl()属性的控件
Response.Write(oStringWriter.ToString());
Response.End();
}
我检查过页面,我的Textbox是放在 <form method=post runat=server> </form> 中的
------解决方案--------------------加
public override void VerifyRenderingInServerForm(Control control)
{
}
即可
详细参见
http://blog.csdn.net/net_lover/archive/2006/09/25/1282106.aspx