我想从另一个下拉列表中绑定下拉列表而不回发 [英] I want to Bind Dropdownlist from another dropdown without postback

查看:64
本文介绍了我想从另一个下拉列表中绑定下拉列表而不回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在没有回发的情况下从另一个下拉列表中绑定下拉列表是否可以使用JAvascript?

解决方案

你可以使用javascript做到这一点但问题是当页面将回发(按钮单击)然后它将创建一个问题。



问题是事件验证。

ASP.NET将尝试在回发之前和之后匹配控件的选项,并在向下拉列表添加新选项元素时抛出错误。


使用此JavaScript示例填充下拉列表。从另一个dropdown onchange事件中调用它:



 <   script     type   =  text / javascript >  
function populateDD2() {
var dd1 = document.getElementById('<% = DD1.ClientID %> ');
var dd2 = document.getElementById('<% = DD2.ClientID %> );
if(dd1.value ==Yes){
var options = [Option 1,Option 2,Option 3];
for(var i = 0; i < options.length; i ++) {

var opt = document.createElement( option);

opt.value < span class =code-keyword> = i;

opt.innerHTML = options [一世];

dd2.appendChild(opt);

< span class =code-attribute> }

}

else {

dd2.options = null; // 无论 else 可能 想要 do。

}

< span class =code-attribute>
}

< / script >





控件:< br $>


 <   asp :DropDownList     runat   =  server    ID   =  DD1    onchange   =  populateDD2() >  
< asp:ListItem 文字 = - 选择 - > < / asp :ListItem >
< asp:ListItem 文字 = > < / asp:ListItem >
< asp:ListItem Text = > < / asp:ListItem >
< / asp:DropDownList >
< asp:DropDownList runat = server ID = DD2 >
< / asp:DropDownList >


I want to Bind Dropdownlist from another dropdown without postback Is it Possible using a JAvascript?

解决方案

You can do that using javascript but the problem is that when the page will postback (on button click) then it will create an issue.

The issue is event validation.
ASP.NET will try to match the control's option after and before postback and will throw an error as you have added new option elements to the dropdown.


Use this JavaScript example to populate a drop down. Call it from another dropdown onchange event:

<script type="text/javascript">
       function populateDD2() {
           var dd1 = document.getElementById('<%=DD1.ClientID %>');
           var dd2 = document.getElementById('<%=DD2.ClientID %>');
           if (dd1.value == "Yes") {
               var options = ["Option 1", "Option 2", "Option 3"];
               for (var i = 0; i < options.length; i++) {

                   var opt = document.createElement("option");

                   opt.value = i;

                   opt.innerHTML = options[i];

                   dd2.appendChild(opt);

               }

           }

           else {

               dd2.options = null;// Or whatever else you might want to do.

           }

       }

   </script>



Controls:

<asp:DropDownList runat="server" ID="DD1" onchange="populateDD2()">
            <asp:ListItem Text="- Select -"></asp:ListItem>
            <asp:ListItem Text="Yes"></asp:ListItem>
            <asp:ListItem Text="No"></asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList runat="server" ID="DD2">
        </asp:DropDownList>


这篇关于我想从另一个下拉列表中绑定下拉列表而不回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆