我用来从转发器打印数据时出现此错误
“只能在Render();”
期间调用RegisterForEventValidation我已经在page指令中设置了EnableEventValidation =“false” 以及我的应用程序在页面标记内的web.config
这是我用于
的代码public static void PrintWebControl(Control ctrl, string Script)
{
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
if (ctrl is WebControl)
{
Unit w = new Unit(50, UnitType.Pixel);
((WebControl)ctrl).Width = w;
}
Page pg = new Page();
if (Script != string.Empty)
{
pg.RegisterStartupScript("PrintJavaScript", Script);
}
HtmlForm frm = new HtmlForm();
pg.Controls.Add(frm);
frm.Attributes.Add("runat", "server");
frm.Controls.Add(ctrl);
string scr = "<script>function window.onafterprint(){history.back(1);}</script>";
htmlWrite.Write(scr);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write("<script>window.print();</script>");
HttpContext.Current.Response.End();
}
答案 0 :(得分:1)
替换
行Page pg = new Page();
与
Page pg = new Page();
pg.EnableEventValidation = false;
答案 1 :(得分:0)
覆盖Render方法,并调用:
Page.ClientScript.RegisterForEventValidation(..)
HTH。