1

In javascript, I want to compare the selected value from a dropdown list to a list of values from my C# code behind. I've tried passing a string list through a HiddenField, but I can't get past the problem of 'Unable to get property 'split' of undefined or null reference'.

Looking around Stackoverflow and elsewhere I've tried many variations of the following code but the 'split error' has me stymied.

<asp:HiddenField ID="HiddenFieldList" runat="server" />

//Code behind:
HiddenFieldList.Value = String.Join(",", ValuesList);

//javascript
var CSVList = document.getElementById("HiddenFieldList").value;
var HFList = [];
HFList = this.CSVList.split(',');

// hoping to get to here...
for(i = 0; i < HFList.length; i++) 
{
   if (DDL == HFList[i])
   {
       do stuff ;
       break ;
    }
 }

Or...instead of assigning a string list to my hidden field should I pass an array?? Fwiw - the project is not MVC.

Edit: My code behind list is derived from values in a datatable column.

5
  • On your ASP.NET page, at the server side, just generate a bunch of text that begins with <script> ends with </script> and has var theArray = [...]' inside. At the client side you'll end up with theArray as a local variable. Commented Jun 7, 2018 at 14:00
  • Please see my edit - bottom line. Commented Jun 7, 2018 at 14:05
  • please add your razor markup Commented Jun 7, 2018 at 14:32
  • Please see my edits Commented Jun 7, 2018 at 14:49
  • Change your code this.CSVList.split(‘,’) to CSVList.split just remove this. Commented Jun 7, 2018 at 15:01

1 Answer 1

2

Change the following line of code

HFList = this.CSVList.split(',');

to

HFList = CSVList.split(',');

because CSVList is local variable.

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

1 Comment

It now works as expected. What is frustrating is that I'd previously tried the same text as you suggested - and still received the error. Thank you for your time.

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.