2014-06-27 17 views
0

我已经使用下面的代码来打印HTML DIV内容&它工作正常,但是当我在Aspx页面中使用Ajax控件时,它给了我一个错误消息,即:在Asp.net中打印aspx文件

“扩展控制 'CalendarExtender2' 不是已注册的扩展 控制扩展控件必须使用() 参数对骂RegisterScriptDescriptors之前 RegisterExtenderControl()进行注册:。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(); 
} 

这里是我的aspx代码

<%@ 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="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> 
        &nbsp;<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="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 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> 

如果我使用frm.Controls.Add(tblOne)代替frm.Controls.Add(divContent)则因为tblone没有一个工作正常Ajax控制。但divContent既有表格也有Ajax控件,它会引发上述异常。我尝试了很多选项,如覆盖OnInitOnPreRender,但对我无效。

+0

你的ASPX页面本身的代码是什么? – Nzall

+0

偏离主题,但不能在javascript中使用2个具有相同名称的函数。事实上,也许你可以,但不可预知哪一个会被调用。 – Nzall

+0

@Nate Kerkhofs先生如果问题是功能bcz然后它会给出一个错误,同时使用,我已经重命名了这两个函数的名称,但仍然存在问题。 –

回答

0

最后我以JavaScript代码&工作对我来说更好的解决了这个问题,我想感谢所有谁给他们的宝贵时间&反馈。 代码在这里:

<script> 
    function Panel1() { 
     var panel = document.getElementById("<%=printablediv.ClientID %>"); 
     var printWindow = window.open('', '', 'height=400,width=800'); 
     printWindow.document.write('<html><head><title>newTable</title>'); 

     printWindow.document.write('</head><body >'); 
     printWindow.document.write(panel.innerHTML); 
     printWindow.document.write('</body></html>'); 
     printWindow.document.close(); 
     setTimeout(function() { 
      printWindow.print(); 

      printWindow.close(); 
     }, 1000); 
     return false; 

    } 
    </script> 
0

这可能是由于您的代码中放置了ToolkitScriptManager。查看这是否解决您的问题:
Error: Extender controls may not be registered before PreRender

+0

我试过这个选项,但它不帮助我。任何其他选项? –

+0

只是预感,但你可以尝试在控件的onInit事件上注册这个事件吗? - http://msdn.microsoft.com/en-us/library/bb353474(v=vs.110).aspx –

+0

Thanx for answer,但它不起作用。 –