开发者

While print data from repeater control in asp.net with C#

开发者 https://www.devze.com 2023-01-30 01:40 出处:网络
I got this error while i use to print data from repeater \"RegisterForEventValidation can only be called during Render();\"

I got this error while i use to print data from repeater

"RegisterForEventValidation can only be called during Render();"

I already set EnableEventValidation="false" in page directive and also in web.config of my application inside page tag

Here is the code which I use for that

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();

    HttpC开发者_Go百科ontext.Current.Response.Write(strHTML);

    HttpContext.Current.Response.Write("<script>window.print();</script>");

    HttpContext.Current.Response.End();

}


Replace the line

Page pg = new Page();       

with

Page pg = new Page();               
pg.EnableEventValidation = false;


Override the Render method, and call:

Page.ClientScript.RegisterForEventValidation(..)

And pass in the options defined here: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerforeventvalidation.aspx

HTH.

0

精彩评论

暂无评论...
验证码 换一张
取 消