views:

226

answers:

3

I have a web site released at facebook platform, i am using C# .Net 2008, the problem is that i am loading an array elements from code behind in javascript arary and i am loading elelment element using the following javascript code :

ArList = new Array('<%=ListOfWords[0]%>','<%=ListOfWords[1]%>','<%=ListOfWords[2]%>','<%=ListOfWords[2]%>');

the problem that when i call an element from the array as follows :

document.getElementById("WordDiv").innerHTML = ArList [0];

The element is not set with the value inspite the array in the code behind has values and i don't know why the value of the array element is not set ? and in some cases i found it loaded with value and everything go right so may be it's a problem in rendering so the value of code behind is not seen in client side ? or where is the problem come ? and when i traced the application in IE i found the status bar report a javascript error then disappered and say done and when the yellow allert appears in the status bar i have clicked it and noticed the message says : object expected.

The problem now is that in onload event of the body tag i call a function in the javascript that intialize the javascript array with values from code behind array and the problem is that in some cases the function is not entered as i traced it by putting an alert in the begining of the function and i found that when the javascript array is not filled the code of the function is not entered as the alert is not showed so i don't know how to force the DOM to enter this function which i call it in the tag here sample of the code: javascript code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
     <html xmlns="http://www.w3.org/1999/xhtml"&gt;
     <head runat="server">         
     </head>
     <body onload="IntializeArr(); return false;">
     <form id="form1" runat="server" >
     ..........Some Controls...............
    </form>
    <script type="text/javascript" language="javascript"> 
    var TList;
    var BList;
       function IntializeArr()
     {       
        TList = new Array('<%=ListofT[0]%>','<%=ListofT[1]%>','<%=ListofT[2]%>','<%=ListofT[3]%>','<%=ListofT[4]%>','<%=ListofT[5]%>','<%=ListofT[6]%>','<%=ListofT[7]%>','<%=ListofT[8]%>','<%=ListofT[9]%>');
        BList = new Array('<%=ListOfB[0]%>','<%=ListOfB[1]%>','<%=ListOfB[2]%>','<%=ListOfB[3]%>','<%=ListOfB[4]%>','<%=ListOfB[5]%>','<%=ListOfB[6]%>','<%=ListOfB[7]%>','<%=ListOfB[8]%>','<%=ListOfB[9]%>');        
     }
    </script>
    </body>
    </html>

C# Code :

public string[] ListOfB = new string[15];
public string[] ListofT = new string[15];

And the code behind array is filled from data returned from database and they are filled i have traced them and each time and they are filled and i found that the problem from javascript i don't know if it is from facebook platform or from my code but i think that it is not from my code as the problem that i call a function in the onload of the tag and the function is not entered and this is the problem so can any one help me please

Hope that i will find a solution as i got depressed

A: 

Wouldn't it be easier to generate this javascript code in your code behind and then call Page.RegisterStartupScript() ? That ensures you have a correct array and you can use less aspx tags in your javascript code.

public string ArrayToJsArray(string[] array)
{
    StringBuilder s = new StringBuilder();
    foreach (var item in array)
    {
        s.Append("'" + Server.HTMLEncode(item) + "',");
    }
    s.Remove(s.Length-1, 1);
}

public void main(){
    string script = "<script type=\"text/javascript\">";
    script += "var TList = new Array(" + ArrayToJsArray(TListArray) + ");";
    script += "var BList = new Array(" + ArrayToJsArray(BListArray) + ");";
    script += "</script>";
    Page.RegisterClientScriptBlock("key", script);
}

Try the above code, forgive me as I haven't run it myself so it may contain bugs but the idea is clear I think.

Peter
i have tried Page.RegisterArrayDeclaration() and nothing modified and still the array not seen from javascript i have thought about this solution but i don't know what is the problem as the silver tag code is not seen from javascript code and sometimes it's working well and got the data and in other times a javascript error occurred and don't see array values
Ahmy
do u mean that i put all the script code in string in the code behind and register it ?i don't understnad how to do that? or u mean to register the array ?at all i have javascript code more than that i wrote in the post and to register it so i will got each function and public variables and put it in the code behind to be passed to the function that register the javscript code? please clarify what do u mean and thanks
Ahmy
I have provided some code that explains what I mean. You create the javascript code to initialize the arrays and then you post them to the page by using registerclientscriptblock.
Peter
A: 

Doesn't .NET have problems rendering aspx tags inside a <script> block?

Are the correct values rendered in the HTML? If not, you should try rendering the whole JS script block in another way.

edeverett
what do u mean with rendering aspx tags inside a <script> block? and correct values rendered in the HTML? the javascript code got the code behind array values and working well in some cases and in other cases don't feel with the code behind array values and not eneter the javascript function and error occurred in the javascript code.
Ahmy
+1  A: 

You need to correctly escape the values:

TList = new Array("<%=Server.HTMLEncode(ListofT[0]%>)", ...);
Darin Dimitrov
Thanks u very very much i am too glad for this answer that helped me too much i am too thankful to u and i need from u an illustration from u why ur code solved the problem ? and why Server.HTMLEncode do ? i hope that u will reply because this will help me in future in understanding manythings and thanks too much
Ahmy
Suppose for example that ListofT[0] = "abc\"abc"; This means that you will end up with the following markup: TList = new Array("abc"abc", ...) which is an invalid javascript and you get an error. Server.HTMLEncode converts " to " and thus you end up with TList = new Array("abc"abc", ...) which now is a valid javascript.
Darin Dimitrov