Are you hitting db on every dropdown selectedindexchanged? if yes. then what you have to do.
Please store your data in viewstate. and on selectedindexchanged event,fill other dropdownlists from your viewstate based on the selected value.
You can use dataset/datatable Select method.
So it's one time activity to store all your data in viewstate in page load.
And with the help of Select method,you can pass values.
For ex:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Rows.Add("1", "Vishal");
dt.Rows.Add("2", "Rajesh");
dt.Rows.Add("3", "Prashant");
dt.Rows.Add("4", "Dharmesh");
dt.Rows.Add("5", "Nitin");
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
ViewState["DataTeable"] = dt;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
DropDownList2.Items.Clear();
if (ViewState["DataTeable"] != null)
{
DataTable dt = (DataTable)ViewState["DataTeable"];
if (dt != null)
{
if (dt.Rows.Count > 0)
{
DataRow[] dr = dt.Select("Id='" + DropDownList1.SelectedValue + "'");
if (dr != null)
{
if (dr.Length > 0)
{
DataTable dt_new = dr.CopyToDataTable();
DropDownList2.DataSource = dt_new;
DropDownList2.DataTextField = "Name";
DropDownList2.DataValueField = "Id";
DropDownList2.DataBind();
}
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
Riyazhaider, if this helps please login to Mark As Answer. | Alert Moderator