Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all
I wanna bypass array of string to the client side code ( javascript) as follow :

C#
protected void Page_Load(object sender, EventArgs e)
       {
           string[] chi = { "sara","mathew" };

           Page.ClientScript.RegisterArrayDeclaration("chi", chi.ToString());
       }


and in client code :

JavaScript
<script type="text/javascript">  
function pp()
        {
            try
            {
                var iChi = new Array(10);
                for (var i = 0; i <chi.length; i++) {
                    iChip.push(chi[i]);
                }
               Rdr.CreatePrint(2,iChi);
            }
            catch (Err) {
                alert(Err.description);
            }
        }

</script>

but i can't access to the chip that defined in code behind in above script.

What I have tried:

when call array in the javascript don't identify array that define on it.
Posted
Updated 18-Jun-16 6:03am
v2
Comments
Sergey Alexandrovich Kryukov 18-Jun-16 13:50pm    
You never ever call JavaScript from the server side. Just think about it: code behind is on the server side, and JavaScript is on the client side. You only pass data from server-side indirectly, by generating content, including JavaScript code, in your HTTP response. This is actually the whole purpose of ASP.NET. In particular, you can include some JavaScript in your ASP.NET file, and use, for example, Razor syntax @{...} (or whatever you use) to write some fragment of JavaScript code calculated out of your server-side data.

—SA
bernova 18-Jun-16 14:31pm    
how do i use chi array in javascript ?! , what have solution ?!!
for example :
http://www.codeproject.com/Questions/554912/sendplusfileplusnameplusformplusclientplussideplus

just i change it to array
an0ther1 19-Jun-16 21:35pm    
Not sure why you are doing this but anyway..
Assuming you have a hidden field on your ASP page that you are populating on the server then you could populate the field with your text & delimit it with a character unlikely to be used in the text - below I have used a ";" character
eg: sara;matthew
Then on the client side, use;
var objText = document.getElementById('<%=HiddenfieldId.ClientID%>');
var objArray = objText.split(";");

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