Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends
i have three drop downs names as Main category,subcategory ,and products which ware displaying values , but i have to select second drop down values on the basis of first drop down values ie when i click or select first drop down on the basis of main category subcategory id has to be shown in second drop down ....

my code is

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default16.aspx.cs" Inherits="Default16" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:gridview ID="Gridview1" runat="server" ShowFooter="true"
            AutoGenerateColumns="false"
            onselectedindexchanged="Gridview1_SelectedIndexChanged"
            onrowdatabound="Gridview1_RowDataBound">
            <Columns>
            <asp:BoundField DataField="RowNumber" HeaderText="Row Number" />

           <asp:TemplateField HeaderText="MainCategory">
                <ItemTemplate>
                    <asp:DropDownList  ID="DropDownList1" runat="server" >
                    </asp:DropDownList >
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="SubCategory">
                <ItemTemplate>
                    <asp:DropDownList  ID="DropDownList2" runat="server" >
                    </asp:DropDownList >
                </ItemTemplate>
            </asp:TemplateField>

           <asp:TemplateField HeaderText="products">
                <ItemTemplate>
                    <asp:DropDownList  ID="DropDownList3" runat="server" >
                    </asp:DropDownList >
                </ItemTemplate>
            </asp:TemplateField>



            <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Quantity">
            <ItemTemplate>
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField >
        <ItemTemplate >

                 <asp:Button ID="ButtonAdd" runat="server"
                             Text="Add New Row"
                             onclick="ButtonAdd_Click" />


        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField >
        <FooterStyle HorizontalAlign="Right" />
            <FooterTemplate>
                 <asp:Button ID="ButtonInsert" runat="server"
                             Text="Insert"
                             onclick="ButtonInsert_Click" />
            </FooterTemplate>
        </asp:TemplateField>

            </Columns>
        </asp:gridview>
    </div>
    </form>
</body>
</html>
Posted
Updated 12-Oct-12 1:59am
v2

Ok this was a bit of a work. So first, you need to bind the main category dropdownlist (sample shown here):

C#
protected void RowBound(object sender, GridViewRowEventArgs e)
        {

            if (((DropDownList)e.Row.FindControl("DropDownList1")) != null)
            {
                ((DropDownList)e.Row.FindControl("DropDownList1")).Items.Add(new ListItem("A", "1"));
                ((DropDownList)e.Row.FindControl("DropDownList1")).Items.Add(new ListItem("B", "2"));
                ((DropDownList)e.Row.FindControl("DropDownList1")).Items.Add(new ListItem("C", "3"));
            }
        }


Now, On the selected index changed event of this DropDownList1, bind the data to the DropDownList2 based on what you select in DropDownList1:

C#
protected void Change(object sender, EventArgs e)
        {
            DropDownList ddlzz = (DropDownList)this.FindControlRecursive(Gridview1, "DropDownList2");

            if (((DropDownList)sender).SelectedValue == "1")
            {
                ddlzz.Items.Add(new ListItem("aaa", "10"));
                ddlzz.Items.Add(new ListItem("aaaa", "100"));
            }
            else if (((DropDownList)sender).SelectedValue == "2")
            {
                ddlzz.Items.Add(new ListItem("bbb", "20"));
                ddlzz.Items.Add(new ListItem("bbbb", "200"));
            }
        }


The main idea here being the method FindControlRecursive that works as follows:

C#
private Control FindControlRecursive(Control root, string controlid)
        {
            if (root.ID == controlid)
            {
                return root;
            }
            else
            {
                foreach (Control ctrl in root.Controls)
                {
                    Control ctrl2return = FindControlRecursive(ctrl, controlid);

                    if (ctrl2return != null)
                    {
                        return ctrl2return;
                    }
                }

                return null;
            }
        }


You do it recursively to find the second drop downlist which is contained in the grid view as another template column and the first dropdown would have no direct way of identifying it.

Remove the onselectedIndexChanged event from the GridView, that is for when you have a select button in your grid which you don't.

Your markup for the DropDownList1 would look like this:

XML
<asp:TemplateField>
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Change"></asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>


You can extend this logic to cover the third drop down as well in the same manner. Hope this helps.
 
Share this answer
 
Try this

C#
protected void MainCategory_SelectedIndexChanged(object sender, EventArgs e)
   {
//find the SubCategory dropdown and bind it according to the MainCategoryID
   }


Thanks
 
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