Click here to Skip to main content
15,895,192 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello friends i have used following code to print a HTML DIV Contents & it works fine, but when i am using a ajax controls in a Aspx page then it gives me error message i.e :-

"Extender control 'CalendarExtender2' is not a registered extender control. Extender controls must be registered using RegisterExtenderControl() before calling RegisterScriptDescriptors(). Parameter name: extenderControl"

C#
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();
}

ASP.NET
<%@ 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 From Date enter into (dd/MM/yyyy) format");
                    document.getElementById('ctl00_ContentPlaceHolder1_txtFDate').focus();
                    return false;
                }
                if (!isDate(Tdate, 'dd/MM/yyyy')) {
                    alert("Please select valid To Date enter into (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 be less than From 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="tt"  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>
                                    From 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>
                                    To 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="tbl2">
                            <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 To PDF" OnClick="btnExport2PDF_Click" />
                                </td>
                                <td>
                                    <asp:Button ID="btnExport2XLS" runat="server" Text="Export To XLS" OnClick="btnExport2XLS_Click" />
                                    <asp:HiddenField ID="hidSort" runat="server" Value="1" />
                                </td>
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
        </div>
    </center>
</asp:Content>


if i am using a frm.Controls.Add(tblOne); instead of frm.Controls.Add(divContent); then it works fine because the tblone doesn't have any ajax control but in divContent both the table as well as Ajax controls have & it throws the above exception, i have tried many options like overriding OnInit, OnPreRender but doesn't work for me.

please give a solution to resolve it...

Thanks in advance
Posted

1 solution

hi,

Please use window.print() in javascript function. Please check by using this and confirm.
 
Share this answer
 
Comments
Datta Salunkhe 27-Jun-14 9:41am    
@Sarvesh Kumar Gupta thanx a lot, yes it works for me but the font of printed page is too small as well as i don't want to print whole page, i just want to print a <Div> tag in aspx page.
please suggest me other options
Datta Salunkhe 30-Jun-14 7:53am    
hello can anyone help me please, still i am facing problem anyone have solution please post it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900