Wednesday, April 16, 2008

Passing arrayList to javascript function







<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Arraylistjavascript.aspx.cs"
Inherits="Arraylistjavascript" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>

<script language="javascript" type="text/javascript">
function Test() {
var listString = document.getElementById('HiddenField1').value;
var listArray = listString.split('~');

// Now you have an array in javascript of each value

for(var i = 0; i < listArray.length; i++) {
alert(listArray[i]);
}

}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
<input type="button" onclick="Test();"
value="Get Value From ArrayList" />
</div>
</form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Arraylistjavascript : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
list.Add("test1");
list.Add("test2");
HiddenField1.Value = ArrayListToString(ref list);

}
private string ArrayListToString(ref ArrayList _ArrayList)
{
int intCount;
string strFinal = "";

for (intCount = 0; intCount <= _ArrayList.Count - 1; intCount++)
{
if (intCount > 0)
{
strFinal += "~";
}

strFinal += _ArrayList[intCount].ToString();
}

return strFinal;
}

}

5 comments:

Alok kumar said...

It is nice article

Android Application Development said...

Hello,great post. Information are pretty exciting and saved me huge amount of time which I have spend on something else instead of searching posts like this. I am waiting for more.

Anonymous said...

Thank you, it really work for me

Anonymous said...

Thanks its excellent

SID said...

Thank you so much lets party !

Post a Comment