代码如下:
    <asp:DropDownList ID="drpTest" AutoPostBack="true" runat="Server">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
    </asp:DropDownList>
    <input type="button" value="MyTest" onclick="MyTest()" />
    <script type="text/javascript">
        var drpTest = document.getElementById("<%= drpTest.ClientID %>");
        function MyTest()
        {
            if (drpTest.selectedIndex == 0)
                drpTest.options[1].selected = true;
            else
                drpTest.options[0].selected = true;
        }
    </script>
我的意思是在javascript中改变drpTest的选中项时,引起drpTest的自动回发(AutoPostBack设置为了“True”),但是始终只有鼠标点选不同选项时,才会回发,点MyTest按钮,drpTest的选项是变了,但是却没有回发现象产生。请问要如何写才能起到作用??

解决方案 »

  1.   

    <script type="text/javascript"> 
            var drpTest = document.getElementById(" <%= drpTest.ClientID %>"); 
            function MyTest() 
            { 
                if (drpTest.selectedIndex == 0) 
                    drpTest.options[1].selected = true; 
                else 
                    drpTest.options[0].selected = true;              document.form1.submit();
            } 
        </script> 
      

  2.   

    我这段代码是要放在UpdatePanel控件中实现局部刷新的,使用document.form1.submit();这种方法,会整个页面回发,我需要的是引发drpTest控件的onchange事件回发实现局部刷新。
      

  3.   

    if (drpTest.selectedIndex == 0) 
                    drpTest.options[1].selected = true; 
                else 
                    drpTest.options[0].selected = true; =>if (drpTest.selectedIndex == 0) 
                    drpTest.options[1].selected = true; 
                else 
                    drpTest.options[0].selected = true; 
    __doPostBack('<%= drpTest.ClientID %>','');
      

  4.   

    控件中的回发处理是首先要用鼠标\键盘在控件上选择选项来触发事件的,然后再判断所选选项是否原来的一致,一致的话会return
    明显你的例子不满足先要条件,所以没有产生回发
    3楼的代码就是自行回发了
      

  5.   

    在CS里改变才执行事件
    调用后台方法
    _doPostBack('drpTest','');
      

  6.   

            function MyTest() 
            {
                var drpTest = document.getElementById("drpTest1");  
                if (drpTest.selectedIndex == 0) 
                    drpTest.options[1].selected = true; 
                else 
                    drpTest.options[0].selected = true; 
                   alert(drpTest.options[0].value) ;
                drpTest.onchange(); 
            }