0

My problem is, I have the following function:

function change_tarif(param) {
    var arrTarif = <% Response.Write(Session["Tarif"]); %>
    .....
}

Session["Tarif"] - contains multidimensional array List and when I'm loading my page it's giving this Error that

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

When I delete <% Response.Write(Session["Tarif"]); %> it doesn't give this error. So, how can I replace this code to get array List from Session["Tarif"] without using the code blocks <% %>?

This is my Array List code in C#:

    public void getTarifList()
{
    int intRows = 0;
    string strTarif = "null";
    Session["Tarif"] = "null";

    SqlCommand cmdTarif = new SqlCommand("sp_SIMPay_GetTarifList");
    cmdTarif.CommandType = CommandType.StoredProcedure;

    DataTable dtTarif = SQL.GetData(cmdTarif);

    if (dtTarif.Rows.Count > 0)
    {
        intRows = dtTarif.Rows.Count;

        strTarif = "new Array(" + intRows + ");";
        int intIndex = 0;

        foreach (DataRow dRows in dtTarif.Rows)
        {
            strTarif += "arrTarif[" + intIndex + "] = new Array('" + dRows["TarifName"] + "', '" + dRows["ProviderID"] + "', '" + dRows["CodeID"] + "');";
            intIndex++;
        }
    }
    else
    {
        strTarif = "null";
    }
    Session["Tarif"] = strTarif;
}

My full script:

function change_tarif(param) {
var  arrTarif = <% Response.Write(Session["Tarif"]); %>
var select = document.getElementById('TarifList');


var i = 0;

if (arrTarif != null) {
    for (i = 0; i < arrTarif.length; i++) {
        if (arrTarif[i][1] == '98' && param == '98') {
            clear_tarif();
            select.options[select.options.length] = new Option('' + arrTarif[i][0] + '', '' + arrTarif[i][2] + '');
            break;
        }
        else {
            clear_tarif();
            select.options[select.options.length] = new Option('Default Tarif', '');
        }
    }
}
}
halfer
  • 19,824
  • 17
  • 99
  • 186
firefalcon
  • 500
  • 2
  • 8
  • 21

2 Answers2

0

You can use Expression (Equivalent to Response.Write()).

function change_tarif(param) {
 var arrTarif = <%= Session["Tarif"] %>;
}

EDIT : I've customized your code,

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    getTarifList();
}
public void getTarifList()
{
    Session["Tarif"] = "null";
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("new Array(" + 3 + ");");
    for(int i=0;i<3;i++)
    {
        sb.AppendLine("arrTarif[" + i + "] = new Array('A" + i + "', 'B" + i + "', 'C" + i + "');");
    }
    Session["Tarif"] = sb.ToString();
}

And markup,

 <script type="text/javascript">
    function change_tarif(param){
       var  arrTarif = <% Response.Write(Session["Tarif"]); %>
       return arrTarif;
      }
     console.log(change_tarif('test'));
 </script>
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • Did you use code blocks <%%> in your container to get the array list? – firefalcon Sep 03 '12 at 02:28
  • AVD, I did what you suggested, but still the same Error. I edited my full script. Maybe something is wrong there? – firefalcon Sep 03 '12 at 07:05
  • Does it throws same error as you've posted? Please add *break* point to verify the result of *stored-proc* and write **console.log** - debug statement to debug JavaScript (open Console Window of Webbrowser). – KV Prajapati Sep 03 '12 at 07:28
  • Yes, it's throwing the same error as before. I put my js function in a separate file scripts.js and replaced <%Response.Write{Session["Tarif"]);%> with '<%Response.Write{Session["Tarif"]);%>' inside the scripts.js file. It's not giving any Error, but it's also not reading my Array List. Any suggestions? – firefalcon Sep 03 '12 at 08:34
0

This happens because somewhere in your page code you do something like (just an example - it doesn't matter that it's a Label here, any dynamically added/removed control would trigger the error):

Label lbl = new Label ();
lbl.Xxxx = ...;

Controls.Add ( lbl ); // this is what triggers your issue

When you have <%= ... %> in the same container where you also try to dynamically add controls through the use of the Controls collection of that container (Page, Panel, etc.), you'll get that error that you saw.

If you can, try to remove the code that dynamically adds/removes the control to the container (the same container where you have the <%= ... %> and then your <%= ... %> block will start working.

To remove the dynamic logic that adds/removes control(s), simply put those controls into the container like any other control, like

<asp:Label runat="server" id="lbl" ... />

and then simply show/hide the control when you need it. This way the control is always present in the page, you won't have to add/remove it during run-time and then you can have code blocks in the same container.

Here's a quick example that would create this error:

...
<div runat="server" id="div1">
    <asp:Label runat="server" id="lbl1" /><br />
    <span>Name:&nbsp;<%= Session["name"] %></span>
</div>
...

// in code-behind
protected override void OnLoad ( EventArgs e )
{
    Label lbl2 = new Label ();
    lbl2.Text = "Time: " + DateTime.Now.ToString ();

    // Error: you cannot modify the Controls collection if
    // the same control contains code blocks
    div1.Controls.Add ( lbl2 );
}
xxbbcc
  • 16,930
  • 5
  • 50
  • 83
  • But I am not adding any Controls through my code. I'm just creating a new array string in code page and trying to retrieve it in my javascript function. – firefalcon Sep 03 '12 at 02:34
  • @Shohin you'd get the same error if you try to use `Controls.Remove()` as well. You can't change the `Controls` collection in any way of a container that also has code blocks. – xxbbcc Sep 03 '12 at 02:36