?
Solved

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

Posted on 2011-04-20
5
Medium Priority
?
979 Views
Last Modified: 2013-12-16
I would like to print a web page.I used the bolew code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using System.Web.SessionState;

public partial class Print : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Control ctrl = (Control)Session["ctrl"];
        PrintHelper.PrintWebControl(ctrl);
    }
}


public class PrintHelper
{
    public PrintHelper()
    {
    }
 
    public static void PrintWebControl(Control ctrl)
    {
        PrintWebControl(ctrl, string.Empty);
    }
 
    public static void PrintWebControl(Control ctrl, string Script)
    {
        StringWriter stringWrite = new StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
        if (ctrl is WebControl)
        {
            Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
        }
        Page pg = new Page();
        pg.EnableEventValidation = false; 
        if (Script != string.Empty)
        {
            pg.ClientScript.RegisterStartupScript(pg.GetType(),"PrintJavaScript", Script);
        }
        HtmlForm frm = new HtmlForm();
        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();
    }
}

Open in new window


in the print button I have the function
 protected void PrintBtn_Click1(object sender, EventArgs e)
        {
            Session["ctrl"] = Panel1;
        ClientScript.RegisterStartupScript(this.GetType(), "onclick", "<script language=javascript>window.open('Print.aspx','PrintMe','height=300px,width=300px,scrollbars=1');</script>");
    

        }

Open in new window


when i run the code and click the print button i get the error
The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).

The reason is that I have a java script function in the aspx page


 <script type="text/javascript">
       function Update() {
           var first = "<%=first %>";
           var second = "<%=second %>";
 ..
..
..

If I replace "<%= with "<%#
and use
Page.Header.DataBind();    in the page load
the print function works but the java script function(update) is not working

How can i make both the functions work?

0
Comment
Question by:asudhaa
5 Comments
 
LVL 9

Expert Comment

by:jkofte
ID: 35431978
can you place the script inside body tag instead of head tag ?
0
 

Author Comment

by:asudhaa
ID: 35432070
This is in a content page and I have placed the code inside the content placeholder

<asp:Content ID="Content1" ContentPlaceHolderID="mainContent" runat="server">
 <script type="text/javascript">
       function Update() {
           var first = "<%=first %>";
           var second = "<%=second %>";
 ..
..
..

0
 
LVL 21

Accepted Solution

by:
Alpesh Patel earned 2000 total points
ID: 35432311
Put the javascript function in .js file and get the reference of that .js file in page.
0
 

Author Comment

by:asudhaa
ID: 35474075
I tried to put the function in a seperate .js file.

But now how do i cal the variable from aspx.cs in to javascript?
 
function Update() {
           var first = "<%=first %>";
           var second = "<%=second %>";

This does not work .How can I get the first and second values from the aspx.cs file in to the .js file
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question