且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何绑定其他下拉列表中的所有值

更新时间:2023-10-06 21:32:40


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>


***的问候
M.Mitwalli


Best Regards
M.Mitwalli


@ Aparna

您可以为类别ddl
使用selectedindex更改的事件 在类别选择的索引更改事件上,您可以绑定产品

请参考

http://www.velocityreviews. com/forums/t89804-re-how-to-capture-selectedindexchanged-event-of-dropdownlist-in-datagrid.html [ http://***.com/questions/1180863/asp-net- c-dropdownlist-selectedindexchanged-event-not-firing [ ^ ]

但不要忘记将您的autopostback = true属性保留在下拉列表中
希望对您有帮助
Hi @Aparna

you can use the selectedindex changed event for the category ddl
on category selected index changed event you can bind the product

Please Refer

http://www.velocityreviews.com/forums/t89804-re-how-to-capture-selectedindexchanged-event-of-dropdownlist-in-datagrid.html[^]

http://***.com/questions/1180863/asp-net-c-dropdownlist-selectedindexchanged-event-not-firing[^]

But don''t forget to keep your autopostback=true prop of your Dropdown list
Hope it will help You


您可以像这样...

1)首先,您需要将类别下拉列表的AutoPostback 属性设置为true,并将一个selected index change event附加到类别下拉列表.

2)现在您只需要绑定Categories下拉列表即可.

3)然后在类别下拉菜单selected index change event中,您需要将您的产品dropdwon与这样的选择查询绑定...

从产品表中选择*,其中类别ID =您所选择的类别的值或文本;

因此这会根据类别下拉列表中的产品下拉列表中返回的过滤数据.
you can go like this...

1) First of all you need to set AutoPostback property of you category dropdown to true, and also attache one selected index change event to categorydropdown.

2) now all you need to bind only Categories dropdown.

3) then in you category dropdown selected index change event you need to bind your product dropdwon with select query like this...

select * from yourproducttable where category id = yourcategorydropdwon''s selected value or text;

so this will return your filtered data based on in your product dropdown based on category dropdown.