Click here to Skip to main content
15,917,627 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I m getting a Code Blocks Error. When i m Press my button in Cs file.

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


In Aspx. File
HTML
<head id="Head1"  runat="server">
    <title>Sales Order</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="../css/style.css">
    <link type="text/css" href="../jQuery/jQuery.UI/Datepicker/CSS/redmond/jquery-ui-1.8.1.custom.css"
        rel="Stylesheet" />
    <link type="text/css" href="../jQuery/Autocomplete/jquery.autocomplete.css" rel="Stylesheet" />
    <link href="../jQuery/jQuery.UI/Tabs/CSS/redmond/jquery-ui-1.8.2.custom.css" rel="stylesheet"
        type="text/css" />
    <link href="../css/template.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="../css/validationEngine.jquery.css" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../jquery/speechbubblesRS.css" />

    <script src="<%# ResolveUrl("../jQuery/jquery.min.js") %>" type="text/javascript"></script>

    <script type="text/javascript" src="<%# ResolveUrl("../js/script.js") %>"></script>

    <script type="text/javascript" src="<%# ResolveUrl("http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js") %>"></script>

    <script type="text/javascript" src="<%# ResolveUrl("../jQuery/jQuery.UI/jquery.ui.core.js") %>"></script>

    <script type="text/javascript" src="<%# ResolveUrl("../jQuery/jQuery.UI/Datepicker/JS/jquery.ui.datepicker.js") %>"></script>

    <script type="text/javascript" src="<%# ResolveUrl("../jQuery/Autocomplete/jquery.autocomplete.pack.js") %>"></script>

    <script type="text/javascript" src="<%# ResolveUrl("../jQuery/Numeric/jquery.numeric.pack.js") %>"></script>

    <script src="<%# ResolveUrl("../jQuery/jQuery.UI/jquery.ui.widget.js") %>" type="text/javascript"></script>

    <script src="<%# ResolveUrl("../jQuery/jQuery.UI/Tabs/JS/jquery.ui.tabs.js") %>"
        type="text/javascript"></script>

    <script src="<%# ResolveUrl("../jQuery/js/jquery.validationEngine-en.js") %>" type="text/javascript"
        charset="utf-8"></script>

    <script src="<%# ResolveUrl("../jQuery/js/jquery.validationEngine.js") %>" type="text/javascript"
        charset="utf-8"></script>

    <script src="<%# ResolveUrl("../jquery/speechbubbles.js") %>" type="text/javascript"></script>

</head>


In Cs File
C#
if (dt.Rows.Count > 0)
            {
                string LnNo = "";
                LnNo += 1;
                hlSOBOMDtls.Attributes.Add("rel", "#dynDivCode" + LnNo);
                hlSOBOMDtls.Attributes.Add("class", "addspeech");

                HtmlGenericControl dynDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                dynDiv.ID = "dynDivCode" + LnNo; dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Gray");
                dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px");

                string dispString = "";
                string AltColor = "FBEFEE";
                dispString = "<table cellspacing="0" cellpadding="2" class="fullborder" style="background-color:FFFFE5;width:100%;font-size:11px;font-family:Verdana;">";

                //dispString = dispString + "<tr style="background-color:#DA5344; color:#FFFFCC;"> " +
                dispString = dispString + "<th class="printHeaderText" style="width:10%;text-align:left;border-removedsolid 1px gray;">LnNo</th>";
                dispString = dispString + " <th class="printHeaderText" style="width:10%;text-align:left;border-removedsolid 1px gray;">Item Name</th>";
                dispString = dispString + " <th class="printHeaderText" style="width:10%;text-align:left;border-removedsolid 1px gray;">Delivery Date</th>";
                dispString = dispString + "</tr>";
                for (int i = 0; i <= dt.Rows.Count - 1; i++)
                {
                    if (AltColor == "FBEFEE") AltColor = "FFFFFF"; else AltColor = "FBEFEE";
                    dispString = dispString + "<tr style="background-color: #" + AltColor + ";"> ";
                    dispString = dispString + " <th class="printHeaderText" style="width:10%;text-align:left;border-removedsolid 1px gray;">" + LnNo + "</th>";

                    if (dt.Rows[0]["ItemName"] != DBNull.Value)
                        dispString = dispString + " <th class="printHeaderText" style="width:100%;text-align:left;border-removedsolid 1px gray; font-weight:lighter">" + dt.Rows[0]["ItemName"].ToString() + "</th>";

                    if (dt.Rows[0]["DeliveryDate"] != DBNull.Value)
                        dispString = dispString + " <th class="printHeaderText" style="width:100%;text-align:left;border-removedsolid 1px gray; font-weight:lighter">" + Convert.ToDateTime(dt.Rows[0]["DeliveryDate"]).ToString("dd-MMM-yyyy") + "</th> ";
                    dispString = dispString + "</tr>";
                }
                dispString = dispString + "</table>";
                dynDiv.InnerHtml = dispString;
                dynDiv.Style.Add(HtmlTextWriterStyle.Visibility, "hidden");
                this.Controls.Add(dynDiv);
            }
Posted
Updated 5-Jan-14 21:40pm
v4
Comments
Karthik_Mahalingam 4-Jan-14 6:38am    
please add in code block, when posting coding stuffs...
Member 10453691 4-Jan-14 7:31am    
Get the Code Blcok Error.
Member 10453691 4-Jan-14 7:37am    
reply
Richard MacCutchan 4-Jan-14 6:40am    
What error, what button, where does it occur?
Member 10453691 4-Jan-14 7:32am    
See i make a Filter PAge when i press my filter Button i get the Code block Error Because of these code.

Your problem may come from the lines like the following one:
C#
dispString = dispString + "<th class="printHeaderText" style="width:10%;text-align:left;border-removedsolid 1px gray;">LnNo</th>";


First, as you are trying to put some double-quotes in a string, you have to esacepe them, so they are not considered as double-quote string delimiters.

But the best option may be to use a StringBuilder here, as you concatenate several strings alltogether ; performance could even be improved.

So your code should look like:
C#
   StringBuilder sb = new StringBuilder("<table cellspacing=\"0\" cellpadding=\"2\" class=\"fullborder\" style=\"background-color:FFFFE5;width:100%;font-size:11px;font-family:Verdana;\">");
   sb.Append("<th class=\"printHeaderText\" style=\"width:10%;text-align:left;border-removedsolid:1px\" mode=\"hold\" />
   sb.Append("<th class=\"printHeaderText\" style=\"width:10%;text-align:left;border-removedsolid:1px=\" mode=\"hold\" />
   sb.Append("<th class=\"printHeaderText\" style=\"width:10%;text-align:left;border-removedsolid:1px\" mode=\"hold\" />

// ... and so on.
</table>

The StringBuilder.AppendFormat(string, params object[]) method could also be useful.

Important thing to remeber is: if you want to insert double-quotes in a string, you have either to escape the double-quote itself (with the '\' character), or double it ('""').

Example:
Imagine you want to store the sentence:
I want to insert the " sign in my string.

in a string.

You can write:
C#
string text = "I want to insert the \" sign in my string.";

OR
C#
string text = "I want to insert the "" sign in my string.";
 
Share this answer
 
v2
Hi Please try this one this will definitely solve your problem.

src="<%# ResolveUrl('../js/script.js') %>"


Just use single quote inside double quote , so you should apply this where you used ResolveUrl Function in that page.
 
Share this answer
 
Comments
Member 10453691 6-Jan-14 4:05am    
it will give a error of Too many characters in character literal
BK 4 code 6-Jan-14 4:20am    
Hi instead of using # use = like this .

src="<%= ResolveUrl('../js/script.js') %>"

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