Click here to Skip to main content
15,847,077 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I Have a Problem with my project ,


I was create 2 dropdownlist ,which value of a dropdownlist set by selected value of another dropdownlist ,

I wish, when i selected a value in dropdownlist1 ,value of dropdownlist2 would be changed

how i can solve this ?

What I have tried:

This is my code ,


in my aspx.cs

protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                Bind_CheckList();
            }
            Listcompany();
           
        }



private void Listcompany()
{
    ddl3.DataSource = comp.GetData();
    ddl3.DataTextField = "Company_Name";
    ddl3.DataValueField = "Company_ID";
    ddl3.DataBind();
    ddl3.Items.Insert(0, new ListItem("--Select--", ""));
}


protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
    string company = ddl3.SelectedValue.ToString();
    ddl1.DataSource = shift.GetDatashiftbycomp(company);
    ddl1.DataTextField = "Name_Shift";
    ddl1.DataValueField = "ID_Shift";
    ddl1.DataBind();
    ddl1.Items.Insert(0, new ListItem("--Select--", ""));
}


in my aspx


 <asp:DropDownList ID="ddl3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl1_SelectedIndexChanged" >
    </asp:DropDownList>
<pre> <asp:DropDownList ID="ddl1" runat="server" AutoPostBack="false" ></asp:DropDownList>
Posted
Updated 16-Mar-23 1:52am

The Listcompany call needs to be within the if (!Page.IsPostBack) block. As it stands, you're re-binding the list every time it posts back, which means the SelectedIndexChanged event will never fire.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        Bind_CheckList();
        Listcompany();
    }
}

You should also give your controls meaningful IDs, instead of just accepting the default values provided by the Visual Studio designer. The IDs ddl1 and ddl3 tell you nothing about what these controls are used for.

It's particularly problematic when your IDs don't match your event handler names - who would be able to guess that ddl1_SelectedIndexChanged handles the SelectedIndexChanged event for ddl3? 🤦‍♂️
 
Share this answer
 
Checkbox checked then will show check box list
 
Share this answer
 
Comments
CHill60 16-Mar-23 8:23am    
This makes no sense

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