如何将下拉列表绑定到另一个下拉列表 [英] how to bind the dropdown list to another dropdown list

查看:128
本文介绍了如何将下拉列表绑定到另一个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个下拉列表,并且我想彼此绑定,例如,如果我在一个下拉列表中选择印度作为我的国家,则另一个应该显示印度的州,如果我在ddl中选择美国,则它应该显示状态美国
请帮助我

i have two drop down list and i want to bind each other like if i select India as my country in one dropdown list the another one should display the states of India and if i select U.S in ddl then it should display the states of U.S.
plz help me out

推荐答案


选中此选项将为您提供想法

Hi
Check this it will give you idea

DataClassesDataContext Db = new DataClassesDataContext();
   protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           var result = from x in Db.cates
                        select x;

           DropDownList1.DataSource = result;
           DropDownList1.DataTextField = "cate1";
           DropDownList1.DataValueField = "id";
           DropDownList1.DataBind();

           var resultItem = from x in Db.Items
                        select x;

           DropDownList2.DataSource = resultItem;
           DropDownList2.DataTextField = "Item_name";
           DropDownList2.DataValueField = "item_code";
           DropDownList2.DataBind();
       }
   }
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       var result = (from x in Db.Items
                    where x.item_code == Convert.ToInt32(DropDownList1.SelectedValue)
                    select x).Single();
//put here as much as u want to bind ddl but make sure u bind ddlist in Load first  then assign SelectedValue .

       DropDownList2.SelectedValue = result.item_code.ToString();

   }







<div>
      <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

          onselectedindexchanged="DropDownList1_SelectedIndexChanged">
      </asp:DropDownList>
      <asp:DropDownList ID="DropDownList2" runat="server">
      </asp:DropDownList>

  </div>


DropDownList1_SelectedIndexChanged事件中,您必须在此处编写代码.
最好的问候
M.Mitwalli


In DropDownList1_SelectedIndexChanged Event you have to write your code here .
Best Regards
M.Mitwalli


它被称为 Cascading Dropdown .

看这里:
CascadingDropDown演示 [使用SQL数据库的AJAX级联下拉示例 [ ^ ]
在选择ASP.NET上填充级联DropDownList [面向初学者的AJAX(第2部分)-使用XMLHttpRequest和jQuery AJAX实施级联下拉列表 [ ^ ]
It''s called Cascading Dropdown.

Look here:
CascadingDropDown Demonstration[^]
AJAX Cascading Dropdown Example using SQL Database[^]
Populate Cascading DropDownList On Selection ASP.NET[^]

If not update panels, then you need to use traditional AJAX - XMLHttpRequest.
Refer:
AJAX for beginners (Part 2) - Using XMLHttpRequest and jQuery AJAX to implement a cascading dropdown[^]


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

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