referencing a dropdownlist in javascript from a codebehind

hopefully i actually remember this in the future, but i’m sure i won’t. there’s a trick to referencing the actual dropdownlist in .net – the select input, that is. if you don’t just want the container, but want to get to the ddl itself, using javascript specifically, you’ll have to pass the DropDownList.DropDownListUniqueID. If you are building your JS in the code[behind/beside], you can use the following for finding and comparing the ddl value.

to pass the id into a JS function, try something like this:


scriptText.Append(“<function>(categoryDropdownID)” & vbCrLf)
scriptText.Append(“var categoryDropdown = document.getElementById(categoryDropdownID);” & vbCrLf)

and, to check if the dropdownlist has a selected value in JS, use something like this:

scriptText.Append(String.Concat(“if(categoryDropdown[categoryDropdown.selectedIndex].value == <value>)”, vbCrLf))

i don’t have the exact source i’m quoting in front of me, but essentially you’re – passing the DropDownListUniqueID into the JS function to be able to reference the object directly in page code. you are then adding the ID to a variable. then you can compare the control to see if it has a selected value. elegant – probably not. will i use it again – probably.

maybe you can use this, i’m sure i will need this later.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s