c# - Printing an aspx file in Asp.net -
i have used following code print html div contents & works fine, when using ajax control in aspx page gives me error message, i.e:
"extender control 'calendarextender2' not registered extender control. extender controls must registered using registerextendercontrol() before calling registerscriptdescriptors(). parameter name: extendercontrol"
here c# code
protected void btnprint_click(object sender, eventargs e) { stringwriter stringwrite = new stringwriter(); system.web.ui.htmltextwriter htmlwrite = new system.web.ui.htmltextwriter(stringwrite); page pg = new page(); pg.enableeventvalidation = false; htmlform frm = new htmlform(); pg.enableeventvalidation = false; pg.controls.add(frm); frm.attributes.add("runat", "server"); frm.controls.add(divcontent); 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(); }
here aspx code
<%@ page title="" language="c#" masterpagefile="~/masters/tsamaster.master" autoeventwireup="true" enableeventvalidation="false" theme="skinfiles" codefile="adminhome.aspx.cs" inherits="masters_default" %> <%@ register assembly="ajaxcontroltoolkit" namespace="ajaxcontroltoolkit" tagprefix="ajaxtoolkit" %> <asp:content id="content1" contentplaceholderid="head" runat="server"> </asp:content> <asp:content id="content2" contentplaceholderid="contentplaceholder1" runat="server"> <script type="text/javascript"> function validatedate() { isvaliddate = validatedate(); if (!isvaliddate) { return false; } else { return true; } } function validatedate() { var fdate = document.getelementbyid('ctl00_contentplaceholder1_txtfdate').value; var tdate = document.getelementbyid('ctl00_contentplaceholder1_txttdate').value; var todaysdate = formatdate(new date(), 'dd/mm/yyyy'); var isvaliddate = true; var flag; if (trim(fdate) != 0 || trim(tdate) != 0) { if (!isdate(fdate, 'dd/mm/yyyy')) { alert("please select valid date enter (dd/mm/yyyy) format"); document.getelementbyid('ctl00_contentplaceholder1_txtfdate').focus(); return false; } if (!isdate(tdate, 'dd/mm/yyyy')) { alert("please select valid date enter (dd/mm/yyyy) format "); document.getelementbyid('ctl00_contentplaceholder1_txtfdate').focus(); return false; } if (((comparedates(fdate, 'dd/mm/yyyy', tdate, 'dd/mm/yyyy')) == 1)) { alert("to date cannot less date "); return false; } return true; } } function trim(str) { return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' '); } </script> <ajaxtoolkit:toolkitscriptmanager runat="server" enablescriptglobalization="true" enablescriptlocalization="true" id="scriptmanager1" /> <center> <div class="divcontainpage" id="divcontent" runat="server"> <table id="tblhead" runat="server"> <tr> <td> <asp:label id="label3" runat="server" text="duedate report" font-bold="true" font-names="verdana" font-size="large" forecolor="#c80000"></asp:label> <br /> <br /> <table id="tbl1" runat="server" style="width: 823px"> <tr> <td> select payment mode: </td> <td> <asp:dropdownlist id="ddlpaymode" runat="server" cssclass="dropdown" width="85px"> <asp:listitem>--select--</asp:listitem> <asp:listitem>cash</asp:listitem> <asp:listitem>cheque</asp:listitem> </asp:dropdownlist> </td> <td> date: </td> <td> <asp:textbox id="txtfdate" runat="server" width="110px"></asp:textbox> <asp:imagebutton id="imgcalendar" runat="server" imageurl="~/images/calendar.png" tabindex="1" /> <ajaxtoolkit:calendarextender id="calendarextender2" runat="server" targetcontrolid="txtfdate" format="dd/mm/yyyy" popupposition="bottomright" popupbuttonid="imgcalendar" /> </td> <td> date: </td> <td> <asp:textbox id="txttdate" runat="server" width="110px"></asp:textbox> <asp:imagebutton id="imgcalendar1" runat="server" imageurl="~/images/calendar.png" tabindex="2" /> <ajaxtoolkit:calendarextender id="calendarextender3" runat="server" targetcontrolid="txttdate" format="dd/mm/yyyy" popupposition="bottomright" popupbuttonid="imgcalendar1" /> </td> <td> <asp:button id="btngo" runat="server" text="go" onclick="btngo_click" onclientclick="return validatedate();" /> </td> <td> <asp:button id="btnreset" runat="server" text="reset" onclick="btnreset_click" width="47px" /> </td> </tr> </table> <table style="width: 600px" runat="server" id="tblone"> <tr> <td> <asp:gridview id="grdstudentinfo" runat="server" skinid="professional" datakeynames="studentid" width="800px" allowsorting="true" autogeneratecolumns="false" backcolor="#deba84" bordercolor="#deba84" borderstyle="none" borderwidth="1px" cellpadding="3" cellspacing="2" onsorting="grdstudentinfo_sorting" allowpaging="true" onpageindexchanging="grdstudentinfo_pageindexchanging" onrowcommand="grdstudent_rowcommand"> <rowstyle backcolor="#fff7e7" forecolor="#8c4510" /> <columns> <asp:boundfield headertext="name" datafield="firstandlastname" sortexpression="firstandlastname"> <itemstyle horizontalalign="left" width="100px" /> <headerstyle horizontalalign="left" width="100px" /> </asp:boundfield> <asp:boundfield headertext="amount due" datafield="firstinstallmentfee" sortexpression="firstinstallmentfee"> <itemstyle horizontalalign="left" width="100px" /> <headerstyle horizontalalign="left" width="100px" /> </asp:boundfield> <asp:boundfield headertext="payment mode" datafield="firstinsmode" sortexpression="firstinsmode"> <itemstyle horizontalalign="left" width="100px" /> <headerstyle horizontalalign="left" width="100px" /> </asp:boundfield> <asp:boundfield headertext="bank name" datafield="firstbankname" sortexpression="firstbankname"> <itemstyle horizontalalign="left" width="200px" /> <headerstyle horizontalalign="left" width="200px" /> </asp:boundfield> <asp:boundfield headertext="cheque number" datafield="firstchequenumber" sortexpression="firstchequenumber"> <itemstyle horizontalalign="left" width="200px" /> <headerstyle horizontalalign="left" width="200px" /> </asp:boundfield> <asp:boundfield headertext="due date" datafield="firstinstallmentdate" sortexpression="firstinstallmentdate"> <itemstyle horizontalalign="left" width="200px" /> <headerstyle horizontalalign="left" width="200px" /> </asp:boundfield> <asp:templatefield headertext="edit"> <itemtemplate> <asp:linkbutton id="lnkedit" runat="server" commandname="editstudent" text="edit" commandargument='<%#eval("studentid")%>'></asp:linkbutton> </itemtemplate> <itemstyle horizontalalign="left" width="200px" /> <headerstyle horizontalalign="left" width="200px" /> </asp:templatefield> </columns> <footerstyle backcolor="#f7dfb5" forecolor="#8c4510" /> <pagerstyle forecolor="#8c4510" horizontalalign="center" /> <selectedrowstyle backcolor="#738a9c" font-bold="true" forecolor="white" /> <headerstyle backcolor="#a55129" font-bold="true" forecolor="white" /> </asp:gridview> </td> </tr> </table> <table id="tbl3" runat="server"> <tr style="height: 30px;"> <td> <asp:button id="btnexport2pdf" runat="server" text="export pdf" onclick="btnexport2pdf_click" /> </td> <td> <asp:button id="btnexport2xls" runat="server" text="export xls" onclick="btnexport2xls_click" /> <asp:hiddenfield id="hidsort" runat="server" value="1" /> </td> </tr> </table> </td> </tr> </table> </div> </center>
if use frm.controls.add(tblone)
instead of frm.controls.add(divcontent)
works fine because tblone
doesn't have ajax control. divcontent
has both table ajax controls, , throws above exception. have tried many options overriding oninit
, onprerender
doesn't work me.
it due placement of toolkitscriptmanager in code. see if resolves problem:
error: extender controls may not registered before prerender
Comments
Post a Comment