Groups | Blog | Home
all groups > dotnet academic > march 2005 >

dotnet academic : Server-Side Client-Side Array



Morphious
3/29/2005 12:35:01 PM
I created a server-side array and now I need to populate it to a client-side
monae
4/5/2005 11:41:04 PM
You can literally write it in or you can use ASP.NET's
RegisterClientSideScript(..). I consider the latter useless. It really does
not save you anything and is cryptic to boot.

public class WebForm1 : System.Web.UI.Page
{
string crlf = Environment.NewLine;
private void Page_Load(object sender, System.EventArgs e)
{
String[]testarray = {"red","blue","green"};
MakeClientSideArray(testarray);
MSExample();
}
// Writing it in...
// If you have a big array, use a StringBuilder object instead of string
// concatenation to build up your string.
private void MakeClientSideArray(String[] csarray)
{
string astr = "";
// Use just the following line for testing before you go on.
//astr += "document.write('Script is OK')" + crlf;
Response.Write("<script language='Javascript'>" + crlf);
astr += "var colors = new Array(1)" + crlf;
for ( int ii = 0 ; ii < csarray.GetLength(0) ; ii++)
{
astr += "colors[" + ii.ToString() + "] = '" + csarray[ii] + "'" + crlf;
}
Response.Write(astr);
//If you have a problem with the following line, separate out the forward
slash
// into a separate string. The MSExample does this.
// I think the problem went away in 1.1 because this works ok for me now
// but used to be a problem in the 1.0 version.
Response.Write("</script>");
}

// The RegisterClientSideScript example as written by Microsoft
// It could not possibly work so I modified it liberally.
// It doesn't have anything to do with arrays, but you could appropriately
modify it.
// To actually call this, you will need to put a button on the form and
// call it in the button's onclick event, which you will have to insert by
hand.
public void MSExample()
{
String scriptString = "<script language=JavaScript> function DoClick() {";
scriptString += "alert('Welcome to Microsoft .NET'); }<";
scriptString += "/";
scriptString += "script>";

if(!this.IsClientScriptBlockRegistered("clientScript"))
this.RegisterClientScriptBlock("clientScript", scriptString);
}
}

[quoted text, click to view]
AddThis Social Bookmark Button