开发者

Select item in CascadingDropDown via JavaScript & invoke update

开发者 https://www.devze.com 2022-12-23 11:39 出处:网络
In code-behind I can do this to select something: // Select item in first DropDownList myCascadingDropDown_1.SelectedValue = itemValue_1+\":::\"+itemText_1;

In code-behind I can do this to select something:

// Select item in first DropDownList
myCascadingDropDown_1.SelectedValue = itemValue_1+":::"+itemText_1;

// Select item in second DropDownList
myCascadingDropDown_2.SelectedValue = itemValue_2+":::"+itemText_2;

How can开发者_StackOverflow社区 I do this in JavaScript?

(I'm aware, that I could search the list and set the selectedIndex property for each dropdown, but I have many items and i'm very lazy.)

EDIT:

npups answer works: I can select my desired item in the first dropdownlist. The problem is however, that new values based on that selected item (it is a CascadingDropDown, remember?) don't show in the second dropdown so I can't select anything there. I would need to somehow invoke the update method of the second dropdown manually: any suggestions?


I was able to solve this:

1) With npups suggestion I was able to set "myCascadingDropDown_1" to my desired value.

2) Using the

myCascadingDropDown_2.CascadingDropDownBehavior._onParentChange(null, null);

method I was able to force the second dropdown to repopulate based on the new selected value of "myCascadingDropDown_1".

3) I wrote a timer to check periodically if the second dropdown has finished repopulation and set the desired value if it has (again using npups answer)..


Check this out:

<select id="foo">
  <option value="bar">bar</option>
  <option value="baz">baz</option>
  <option value="bork">bork</option>
</select>

<script type="text/javascript">
  var selectElem = document.getElementById('foo');
  selectElem.value = 'baz';
</script>

Setting the value of the select element fixes that.

Firefox gets it right even if you just use the tags' content as values (instead of specifying a value in the option's value attribute). Not sure about other browsers.

EDIT, more stuff:
So there is some other select (or equivalent) that is updated by some harmonizing function when the first select changes?
Here, i have it in the first select's onchange. When the selected element is set with this "value-setting" technique, the onchange isn't triggered. Though, one can call the harmonizing function manually when you change the first select. I show two different ways (both in comments) below.

<select id="foo" onchange="harmonize();">
  <option value="bar">bar</option>
  <option value="baz">baz</option>
  <option>bork</option>
</select>

<select id="foo2">
 <option value="0">This</option>
 <option value="1">That</option>
</select>


<script type="text/javascript">
   var select0 = document.getElementById('foo');
   var select1 = document.getElementById('foo2');
   select0.value = 'baz';
   // alternative 1: call harmonize();
   // alternative 2: call select0.onchange();

   function harmonize() {
     if (select0.value==='baz') {
       select1.value = '1';
     }
     else {
       select1.value = '0';
     }
   }
</script>

I didn't bother to hide global variables etc. here, but of course that is a good idea.


you can also use myCascadingDropDown_2.set_SelectedValue(resultval, resultVal) instead of timer (item3)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号