5

How Can i fill an array that defined in javascript with c# in behind code?

EDIT:

here is my code

protected void Page_Load(object sender, System.EventArgs e)
{
string[] locations = new string[] {
 "Las Vegas",
 "Los Angeles",
 "Tampa",
 "New York",
 "s",
 "sss"
};
string jsArray = GetJSArrayForVBArray(locations);
this.ClientScript.RegisterArrayDeclaration("usernames", jsArray);
}
private string GetJSArrayForVBArray(string[] vbArray)
{
StringBuilder myResult = new StringBuilder();
foreach (string item in Constants.vbArray) {
 {
 myResult.Append(",'" + item + "'");
 }
}
if ((myResult.Length > 0)) {
 return myResult.ToString().Substring(1);
} else {
 return "";
}
}

Javsacript:

<script type="text/javascript">
 $(function () {
 var usernames = new Array();
 $("#tags").autocomplete({
 source: usernames
 });
 });
</script>
Oded
500k102 gold badges899 silver badges1k bronze badges
asked Aug 7, 2010 at 9:18
6
  • Generate a javascript code block to do it. someArray.push(someValue). Commented Aug 7, 2010 at 9:25
  • Also, check if you can accept a few answers to your earlier questions. Asking questions and not coming back to them is rude. Commented Aug 7, 2010 at 9:27
  • what is the problem with syntax highlighter? @tomalak ??????????? @ Jaroslav Jandek : thanks. give me a link for more information or an example @oded edited Commented Aug 7, 2010 at 9:30
  • @shaahin: I don't think that there are simpler words to explain it. Read my comment again if you don't understand it. Commented Aug 7, 2010 at 9:44
  • @Tomalak: I did that. Thanks For your advice :) . Commented Aug 7, 2010 at 9:55

2 Answers 2

5

use the JavaScriptSerializer class. Something like the following should do it

protected void Page_Load(object sender, System.EventArgs e)
{
 string[] locations = new string[] {
 "Las Vegas",
 "Los Angeles",
 "Tampa",
 "New York",
 "s",
 "sss"
 };
 JavaScriptSerializer serializer = new JavaScriptSerializer();
 string jsArray = serializer.Serialize(locations);
 this.ClientScript.RegisterClientScriptBlock(this.GetType(), "locations", jsArray, true);
}
answered Aug 7, 2010 at 9:30

4 Comments

Thanks a lot. how can i use it in my aspx page?
I cant use array in aspx page. please help me. i used this code. <script type="text/javascript"> $(function () { var locations= new Array(); $("#tags").autocomplete({ source: locations }); }); </script>
@shaahin - what doe my code output to the page? There's no need to declare a new JavaScript array to hold the values that we are outputting from the server side, we just need to assign what we output to a variable and then assign that variable value to the source property of the object that you're passing to autocomplete
Alternatively, you could write the result of calling serialize straight into the aspx markup, using <%= ... %> tags
4

Sounds like a job for JSON. Note that if you scroll down on that page you'll see a number of resources for utilizing JSON in C#. It's really a great way to transfer data back and forth between various platforms/languages.

answered Aug 7, 2010 at 9:28

1 Comment

Well, check out the links listed on that page. Most of the work for 'converting' is done for you by one of those packages, depending on your implementation. Specifically, it becomes a matter of basically calling JsonEncode/JsonDecode (or using helper functions to work with specific collection types) with the code at: techblog.procurios.nl/k/618/news/view/14605/14863/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.