Hi, I am trying to build a 3-lvl accordion bootstrap and bind data to Datalist with
Code behind
protected void GetData()
{
using (SqlConnection conn = new SqlConnection(connString))
{
string sqlQuery = "SELECT DISTINCT IdOB,Obiectiv FROM tblObiectiv ORDER BY Obiectiv";
using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
{
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
conn.Open();
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
conn.Close();
}
}
}
private static DataTable GetDataTable(string query)
{
string constr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = query;
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = conn;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
}
}
}
protected void DataList1_ItemCommand(Object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Obiectiv" && e.Item.ItemType == ListItemType.Item)
{
int idob = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
DataRowView drv = e.Item.DataItem as DataRowView;
DataList innerDataList = e.Item.FindControl("DataList2") as DataList;
innerDataList.DataSource = GetDataTable("SELECT DISTINCT IdCL,Cladire FROM tblCladire WHERE IdOB ='" + idob + "'");
innerDataList.DataBind();
}
}
protected void DataList2_ItemCommand(Object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Cladire" && e.Item.ItemType == ListItemType.Item)
{
DataRowView drv = e.Item.DataItem as DataRowView;
DataList secondDataList = e.Item.FindControl("DataList2") as DataList;
int idcl = Convert.ToInt32(secondDataList.DataKeys[e.Item.ItemIndex].ToString());
secondDataList.DataSource = GetDataTable("SELECT DISTINCT IdCA,Camera FROM tblCladire WHERE IdCL ='" + idcl + "'");
secondDataList.DataBind();
}
}
but I only get data for "Obiectiv" but not the other lvls. No error also, how it should be done?
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.

Marius VasilePosted Nov 10, 2024, 1:15 PM
I gave up accordion and tried with div's. However, I can't make third level visible and the link works only for first value like Datalist2 on item command is not working at all