ThisIsSteve

Entries from December 2008

referencing a dropdownlist in javascript from a codebehind

December 13, 2008 · Leave a Comment

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:

<function>(Me.<DropDownList>.DropDownListUniqueID)

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.

Categories: code