Hi, I am trying to build a 3-lvl accordion bootstrap and bind data to Datalist with
SQL
' />
' CommandName="Obiectiv" UseSubmitBehavior="false" Style="font-size: 11px;" class="form-control form-control-sm" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne" />
' />
' CommandName="Cladire" UseSubmitBehavior="false" Style="font-size: 11px;" class="form-control form-control-sm" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="true" aria-controls="collapseTwo" />
' />
' CommandName="Camera" UseSubmitBehavior="false" Style="font-size: 11px;" class="form-control form-control-sm" data-toggle="collapse" data-target="#collapseThree" aria-expanded="true" aria-controls="collapseThree" />
Code behind
C#
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?


Marius VasileRank #785 · 2.9K reputationPosted Nov 10, 2024, 8:15 AM EST
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
' /> ' CommandName="Obiectiv" UseSubmitBehavior="false" Style="font-size: 11px;" class="form-control form-control-sm" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne" /> ' /> ' CommandName="Cladire" UseSubmitBehavior="false" Style="font-size: 11px;" class="form-control form-control-sm" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="true" aria-controls="collapseTwo" /> ' /> ' CommandName="Camera" UseSubmitBehavior="false" Style="font-size: 11px;" class="form-control form-control-sm" data-toggle="collapse" data-target="#collapseThree" aria-expanded="true" aria-controls="collapseThree" />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()); DataList innerDataList = e.Item.FindControl("DataList2") as DataList; innerDataList.DataSource = GetDataTable("SELECT DISTINCT IdCL,Cladire FROM tblCladire WHERE IdOB ='" + idob + "'"); innerDataList.DataBind(); HtmlControl theDivS = (HtmlControl)e.Item.FindControl("DivSecundar"); theDivS.Attributes["class"] = theDivS.Attributes["class"].Replace("hidden", "").Trim(); } } protected void DataList2_ItemCommand(Object source, DataListCommandEventArgs e) { if (e.CommandName == "Cladire" && e.Item.ItemType == ListItemType.Item) { DataList innerDataList = e.Item.FindControl("DataList2") as DataList; int idcl = Convert.ToInt32(innerDataList.DataKeys[e.Item.ItemIndex].ToString()); DataList secondDataList = e.Item.FindControl("DataList3") as DataList; secondDataList.DataSource = GetDataTable("SELECT DISTINCT IdCA,Camera FROM tblCamera WHERE IdCL ='" + idcl + "'"); secondDataList.DataBind(); HtmlControl theDivT = (HtmlControl)e.Item.FindControl("DivTertiar"); theDivT.Attributes["class"] = theDivT.Attributes["class"].Replace("hidden", "").Trim(); } }