CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Dynamically Add JavaScript Arrays in ASP.Net

By Satheesh Babu
Posted On Nov 27,2010
Article Rating:
Be first to rate
this article.
No of Comments: 1
Category: ASP.Net/JavaScript
Print this article.

Dynamically Add JavaScript Arrays in ASP.Net

 

JavaScript and doing client side manipulation is one of unavoidable things in any web based applications we develop. Sometimes, we may have requirements where we need to declare or register a JavaScript array from the asp.net code behind file. For example, we may need to send an array of data from server to client side for some manipulations in JavaScript. Moving forward, this little article will help us to declare a JavaScript array in output HTML in ASP.Net.

To understand better, we will develop a small picture album application where we will create a JavaScript array of image urls in code behind file and will emit it to the HTML output.

 

How to declare JavaScript array from code behind?

The ClientScriptManager class has packed with a method called RegisterArrayDeclaration() which can be used to register a JavaScript array from code behind file. The ClientScript property of Page object will expose the instance of ClientScriptManager object. The below code will register a JavaScript array of image urls.

Picture Album using Javascript Arrays

protected void Page_Load(object sender, EventArgs e)

    {     

        String arrName = "AlbumPicArray";

        String arrValue = "'/jsArrDemo/cartoons/1.bmp',
'/jsArrDemo/cartoons/2.bmp','/jsArrDemo/cartoons/3.bmp',
'/jsArrDemo/cartoons/4.bmp',
'/jsArrDemo/cartoons/5.bmp'";

        ClientScriptManager cs = Page.ClientScript;

        cs.RegisterArrayDeclaration(arrName, arrValue);

 

    }

When executed, the RegisterArrayDeclaration() will check whether a registered array exists with the same name as specified in the arrayName parameter and, if so, it will add the values specified in the arrayValue parameter else, it will create a new JavaScript array with the name and value specified. Once executed, you can see the JavaScript array declared in the html output. Refer below,

 

<script type="text/javascript">

//<![CDATA[

var AlbumPicArray =  new Array('/cartoons/1.bmp','/cartoons/2.bmp','/cartoons/3.bmp','/cartoons/4.bmp','/cartoons/5.bmp');

//]]>

</script>

Now, you can go ahead and use the above JavaScript array and can do your client side manipulations. In this article, I will use jQuery library to implement the picture album using JavaScript array. If you are new to jQuery library then I recommend you to take a quick look on the below FAQ’s to understand this article better.

What is jQuery? How to use it in ASP.Net Pages?

How to enable jQuery intellisense in Visual Studio 2008?

How to use jQuery intellisense in an external javascript file?

 

In this sample, we will provide a feature where user can click Next and Previous link to navigate picture album. Something like below,

How to create javascript arrray dynamically using C# ?

The below jQuery code will help you to achieve the same.




    <script src="_scripts/jquery-1.4.3.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        var i = 0;

        $(function() {

            $("#imge").attr("src", AlbumPicArray[0]);

 

            $("#aNext").click(function() {

                if (i < AlbumPicArray.length - 1) {

                    i++;

                }

                else {

                    i = 0;

                }

                $("#imge").attr("src", AlbumPicArray[i]);

            });

            $("#aPrev").click(function() {

                if (i > 0) {

                    i--;

                }

                else {

                    i = AlbumPicArray.length - 1;

                }

                $("#imge").attr("src", AlbumPicArray[i]);

            });

 

        });

   

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div>

<a href="#" id="aPrev">Prev</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" id="aNext">Next</a><br />

    <img id="imge" />

</div>

    </form>

</body>

</html>

Execute the page and see it in action.

Conclusion

Array is one of the best way to send multiple data in any programming language. There will be always need where we need send data to client side in ASP.Net. In this case, we mostly use HiddenField control to satisfy the requirement. Thus, from this article we have understood how to declare a JavaScript array from the code behind file which is another nice way of sending multiple data from server in ASP.Net.

Happy Coding!!

 

 

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
mcnm
bnmcbmc