ATT ATT - 2 months ago 12
ASP.NET (C#) Question

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

I am using Asp.net c# with javascript

I want use the Session value in javascript so am using this code:

<script type="text/javascript">
var Warehousename = '<%= Session["warehouse_id"] %>';
var temp = new Array();
temp = Warehousename.split(",");
if (temp.length > 1) {
alert('you have multiple access of warehouse. Kindly select the only one warehouse from Preference')
window.location.href = "frm_preferences.aspx";
}
</script>


But am facing some error like


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


So where am doing wrong please suggest me

Div Div
Answer

Here are possible solutions:

First :
Remove JavaScript from the header section of page and add it to body of the page and run your application it will work for you!

Second :

Replace the code block with <%# instead of <%=

After replace code block with <%# instead of <%= add following code in page load

protected void Page_Load(object sender, EventArgs e)
{
  Page.Header.DataBind();    
}