简体   繁体   中英

to populate one dropdown on selectedindexchanged of another inside gridview

I have gridview with two dropdowns in a row say country and state,on change of country dropdown i want to populate the state dropdown when the grid is in edit state. I got both the dropdowns in RowEditing event of grid,also selectedindexchanged event is attached to first dropdown of grid.The problem is how to get second dropdown ie state dropdown in selectedindexchange event of country dropdown.

If RowEditing event occurs before dropdown selection changed event, you can store reference to current Row in you application. Then get this row in dropdown changed event.

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl1 = (DropDownList)sender;
        GridViewRow row = (GridViewRow)ddl1.NamingContainer;
        if (row != null)
        {`enter code here`
            DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList3");
            {
                //call the method for binding the second DDL based on the selected item on the first DDL
                DataTable dt = BindDropDownList(ddl1.SelectedItem.Text);
                ddl2.DataTextField = "Field1";
                ddl2.DataValueField = "Field2";
                ddl2.DataBind();
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM