使用C从asp.net中的repeater控件打印数据时#
我在从中继器打印数据时出错 “RegisterForEventValidation只能在Render()期间调用;” 我已经在page指令中设置了EnableEventValidation=“false” 也在我的应用程序的web.config中的页面标记内 这是我用来做这件事的代码使用C从asp.net中的repeater控件打印数据时#,asp.net,Asp.net,我在从中继器打印数据时出错 “RegisterForEventValidation只能在Render()期间调用;” 我已经在page指令中设置了EnableEventValidation=“false” 也在我的应用程序的web.config中的页面标记内 这是我用来做这件事的代码 public static void PrintWebControl(Control ctrl, string Script) { StringWriter stringWrite = new Strin
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();
}
publicstaticvoidprintwebcontrol(控件ctrl,字符串脚本)
{
StringWriter stringWrite=新建StringWriter();
HtmlTextWriter htmlWrite=新的HtmlTextWriter(stringWrite);
如果(ctrl为网络控制)
{
单位w=新单位(50,单位类型.像素);
((网络控制)ctrl).Width=w;
}
第pg页=新页();
if(脚本!=string.Empty)
{
pg.RegisterStartupScript(“PrintJavaScript”,脚本);
}
HtmlForm frm=新的HtmlForm();
pg.Controls.Add(frm);
添加(“runat”、“server”);
frm.Controls.Add(ctrl);
string scr=“function window.onafterprint(){history.back(1);}”;
htmlWrite.Write(scr);
第页设计初始化();
pg.RenderControl(htmlWrite);
string strHTML=stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write(“window.print();”);
HttpContext.Current.Response.End();
}
覆盖渲染方法,并调用:
Page.ClientScript.RegisterForEventValidation(..)
并传入此处定义的选项:
HTH.更换管路
Page pg = new Page();
与
Page pg = new Page();
pg.EnableEventValidation = false;