1

I write a code that gives value from database and saves them in a list and I put list into array and send it to ASP hiddenField. I want to pass this data to JavaScript var as array. How can I do it? I want to save values as string in array.

aspx:

<asp:HiddenField ID="hdnCity_BillMonth" runat="server" />
<asp:HiddenField ID="hdnCount_BillMount" runat="server" />

aspx.vb:

 hdnCity_BillMonth.Value = String.Join(",", City)
 hdnCount_BillMount.Value = String.Join(",", Value)

City and Value are List

 var City_BillMounth ;
 var Count_BillMounth;
13
  • are you using .net mvc frame work? Commented May 24, 2015 at 6:11
  • u got the list values in coma separated form ?then just assign the hidden field value to a variable in js. like var hdnvalue=document.getElementById("hiddenfieldid"); then split the string using comma like var samar=hdnvalue.split(',') Commented May 24, 2015 at 6:12
  • @ArunprasanthKV my hiddenfield value in html is like this: value="a,b,c" Commented May 24, 2015 at 6:13
  • @ArunprasanthKV i use asp.net classic Commented May 24, 2015 at 6:13
  • did you got the output ? let me know if you still have any problems ? Commented May 24, 2015 at 6:23

1 Answer 1

1

Since you're joining city values with a "," symbol you can easily convert it into a java script array using split like this:

I assume you are getting your hdnCity_BillMonth values like "city1, city2,city3,...cityN"

 var City_BillMounth = [];
 City_BillMounth = hdnCity_BillMonth.split(",");
 console.log("city Array: "+ City_BillMounth );

do the same for the hdnCount_BillMount as well.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks. But I debug It and throw this error: TypeError message:"document.getElementById(...) is null" I use: var Count_BillMounth = [] Count_BillMounth = document.getElementById('ContentPlaceHolder1_hdnCount_BillMount').split(",");
I see this in html: <input type="hidden" name="ctl00$ContentPlaceHolder1$hdnCity_BillMonth" id="ContentPlaceHolder1_hdnCity_BillMonth" value="city1,city2,city3,city4,city5" />
same error :( But I solve that by your idea.. tnx very much :)

Your Answer

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.