من از تابع printhelper که خانم bahar_enginer در سایت گذاشتن برای پرینت از محتوای یک پنل که حاوی یک گرید ویوو است،استفاده می کنم:
publicstaticvoid PrintWebControl(Control ctrl, string Script)
{
StringWriter stringWrite = newStringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
if (ctrl isWebControl)
{
Unit w = newUnit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
}
Page pg = newPage();
pg.EnableEventValidation = false;
if (Script != string.Empty)
{
pg.ClientScript.RegisterStartupScript(pg.GetType() , "PrintJavaScript", Script);
}
HtmlForm frm = newHtmlForm();
Panel pnl = newPanel();
pg.Controls.Add(frm);
frm.Attributes.Add("runat", "server");
frm.Controls.Add(ctrl);
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();
}
اما در خط زیر ارور میده:
frm.Controls.Add(ctrl);
من این تابع رو در رویداد کلیک image button ام فراخوانی می کنم:
Session["ctrl"] = Panel1;
Control ctrl = (Control)Session["ctrl"];
PrintHelper.PrintWebControl(ctrl);
و در سورس همین صفحه (که یک صفحه ascx است) یک اسکریپت هم جهت پرینت استفاده کردم:
<scriptlanguage="javascript"type="text/javascript">
<!--
function imgbtnPrint_onclick() {
//open new window set the height and width =0,set windows position at bottom
var a = window.open ('','','left =' + screen.width + ',top=' + screen.height + ',width=0,height=0,toolbar=0,scrollbars=0,status=0 ');
//write gridview data into newly open window
a.document.write(document.getElementById('<%= GridView1.ClientID %>').innerHTML);
a.document.close();
a.focus();
//call print
a.print();
a.close();
returnfalse;
}
// -->
</script>
و یک صفحه پرینت هم دارم که در رویداد لودش، دستور زیر رو نوشتم:
Control ctrl = (Control)Session["ctrl"];
PrintHelper.PrintWebControl(ctrl);
امیدوارم منظورمو درست رسونده باشم. اگه نه، بگین تا صفحه کدم رو بذارم.
ممنون میشم اگه راهنماییم کنید.
با تشکر فراوان