Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i want to select the products depending upon the categories....i used two drop downs one for category and one for products....i want to select the product by selecting the category...help me
Posted

Hi ,
C#
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();

   }


ASP.NET
<div>
      <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
          onselectedindexchanged="DropDownList1_SelectedIndexChanged">
      </asp:DropDownList>
      <asp:DropDownList ID="DropDownList2" runat="server">
      </asp:DropDownList>

  </div>

Best Regards
M.Mitwalli
 
Share this answer
 
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://stackoverflow.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
 
Share this answer
 
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.
 
Share this answer
 
hey,

refer this link it will help u lot
http://www.webdeveloper.com/forum/showthread.php?t=224006[^]

Best Luck
 
Share this answer
 
Just Google/CP Search for 'Cascading Dropdown'.

What you are looking for is populating a second dropdown based on the value selected in first dropdown.

If not anything else, Ajaxtoolkit has a cascading dropdown[^] control.

[You have not mentioned if Web or Winforms, above example is for ASP.NET]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900