On my project I have to use three dropdownlists and the second one is populated from item selected from first one.
The problem exists there while I'm selecting a row from GridView. I can send those values to dropdownlist but the first DropDownList is not populating the second one if I select a row in gridview
The property of first DDL is set to IsPostBack because when I select an Item, it must populate the second DDL.
The code is as following:
DDL1
public void ShowDDL1_Items()
{
SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
try
{
using (sqlConn)
{
SqlCommand sqlCmd = new SqlCommand("procDDL1", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add(new SqlParameter("@ID_DDL1", SqlDbType.Int));
sqlCmd.Parameters["@ID_DDL1"].Value = Convert.ToInt32(Session["ID_Shkolla"]);
sqlConn.Open();
SqlDataReader reader = sqlCmd.ExecuteReader();
if (reader.HasRows)
{
DDL1.DataSource = reader;
DDL1.DataTextField = "FieldName";
DDL1.DataValueField = "ID_Field";
DDL1.DataBind();
DDL1.Items.Insert(0, new ListItem("--Select--", "0"));
DDL2.Items.Insert(0, new ListItem("--Select--", "0"));
DDL3.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
}
catch (Exception ex
{
throw ex;
}
finally
{
sqlConn.Close();
}
}
The DDL2
protected void DDL1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection sqlConn = new SqlConnection(StringKoneksioni.Stringu);
try
{
using (sqlConn)
{
SqlCommand sqlCmd = new SqlCommand("procDDL2", sqlConn);
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlCmd.Parameters.Add(new SqlParameter("@ID_Field", SqlDbType.Int));
sqlCmd.Parameters["@ID_Field"].Value = Convert.ToInt32(ddListDrejtimi.SelectedValue);
sqlConn.Open();
SqlDataReader reader = sqlCmd.ExecuteReader();
if (reader.HasRows)
{
DDL2.DataSource = reader;
DDL2.DataTextField = "DDL2FieldName";
DDL2.DataValueField = "ID_Field2";
DDL2.DataBind();
DDL2.Items.Insert(0, new ListItem("--Select--", "0"));
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
sqlConn.Close();
}
}
How to populate DD2 if I select a row in gridview
protected void GV1_SelectedIndexChanged(object sender, EventArgs e)
{
DDL1.SelectedValue = GV1.SelectedRow.Cells[5].Text;
DDL1.SelectedItem.Value = HttpUtility.HtmlDecode(GV1.SelectedRow.Cells[6].Text.ToString());}
Thank you in advance for your reply.