简体   繁体   English

EntityDataSource和DropDown

[英]EntityDataSource and DropDown

I have two dropdownlist in my project that bind from EntityDataSource. 我的项目中有两个下拉列表,它们是从EntityDataSource绑定的。 second Dropdownlist must bind base on first DropDownlist. 第二个Dropdownlist必须基于第一个DropDownlist进行绑定。 How can I do this? 我怎样才能做到这一点?

问题

If you are using ASP.net AJAX, AJAX control toolkit has a control called "Cascading DropDown", you can use it. 如果您使用的是ASP.net AJAX,则AJAX控件工具箱中有一个名为“ Cascading DropDown”的控件,您可以使用它。

MS Sample MS样品

You can do it like this if you need no postbacks: 如果不需要回发,可以这样操作:

<select id="s1" >
    <option>-----</option>
    <option>item 1</option>
    <option>item 2</option>
    <option>item 3</option>
</select>

<br />

<select id="s2" >
</select>​

<script type="text/javascript">
    var s1 = document.getElementById("s1");
    var s2 = document.getElementById("s2");

    s1.onchange = function () {
        s2.options.length = 0;
        var value = this.value;

        if (value != "-----") {
            var opt = document.createElement("OPTION");
            opt.text = "sub " + value;
            opt.value = "sub " + value;
            s2.options.add(opt);
        }
    }
</script>

Or you can do the same kind logic on the server side but this will bring postbacks to the scene. 或者,您也可以在服务器端执行相同的逻辑,但这会导致回发。

Assume Entity1 with ID, Name as properties and Entity2 with ID,Entity1ID,OrderNumber as properties. 假定以ID,名称作为属性的Entity1和以ID,Entity1ID,OrderNumber作为属性的Entity2。

Bind 绑定

`Dropdownlist1.Datasource = list(of Entity1)
 Dropdownlist1.DataValueField = "ID"
 Dropdownlist1.DataTextField = "Name"
 Dropdownlist1.databind()`

On selectedvalue change event of dropdownlist1 check whether user has selected valid value and then do linq search from Entity2 datasource and bind it to Dropdownlist2. 在dropdownlist1的selectedvalue更改事件上,检查用户是否选择了有效值,然后从Entity2数据源进行linq搜索并将其绑定到Dropdownlist2。

You can also go for cascading dropdownlist as mentioned by Avani. 您还可以按照Avani的说明去级联下拉列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM