locked
DropDown RRS feed

  • Question

  • User-797751191 posted

    Hi

      I have 2 dropdown & i want on click / change of dropdown , values also get changed in other dropdown . Data comes from Database in both dropdown.

    Thanks

    Saturday, September 7, 2019 3:54 AM

All replies

  • User288213138 posted

    Hi jsshivalik,

    According to your description, I couldn’t understand your requirement clearly.

    Do you want to make a Cascading-DropDown?

    if so, you can refer to below code.

    I filled dr2 with different fields, depending on the select value of dr1.

    you can also try AJAX-Cascading-DropDown.

    If I misunderstand your requirement, please post more details information about your requirement.

    The code:

    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
                <asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
    
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    BindDr1();
                }
            }
            public string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            public void BindDr1()
            {
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand("SELECT * FROM Dr1"))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);
                                DropDownList1.DataSource = dt;
                                DropDownList1.DataTextField = "Name";
                                DropDownList1.DataTextField = "Name";
                                DropDownList1.DataBind();
                            }
                        }
                    }
                }
            }
            public void BindDr2(string s)
            {
                string query = string.Format("SELECT {0} FROM Dr2 ", s); ;
                using (SqlConnection con = new SqlConnection(constr))
                {
                    using (SqlCommand cmd = new SqlCommand(query))
                    {
                        using (SqlDataAdapter sda = new SqlDataAdapter())
                        {
                            cmd.Connection = con;
                            sda.SelectCommand = cmd;
                            using (DataTable dt = new DataTable())
                            {
                                sda.Fill(dt);
                                DropDownList2.DataSource = dt;
                                DropDownList2.DataTextField = s;
                                DropDownList2.DataTextField = s;
                                DropDownList2.DataBind();
                            }
                        }
                    }
                }
            }
    
            protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                    BindDr2(DropDownList1.SelectedValue);         
            }
    

    The result:

    Best regards,

    Sam

    Monday, September 9, 2019 2:37 AM