I have three nested datalists. On item command from first datalist thigs works well. On item command from second datalist things are not working well. How it should be done, please?
SQL
' />
' CommandName="Obiectiv" UseSubmitBehavior="false" Style="font-size: 11px;background-color:lightgreen" class="form-control form-control-sm" />
' />
' CommandName="Cladire" UseSubmitBehavior="false" Style="font-size: 11px;background-color:lightsalmon" class="form-control form-control-sm" />
' />
' CommandName="Camera" UseSubmitBehavior="false" Style="font-size: 11px;background-color:lightblue" class="form-control form-control-sm" />and code behind
C#
protected void DataList1_ItemCommand(Object source, DataListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
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("DivS");
theDivS.Attributes["class"] = theDivS.Attributes["class"].Replace("hidden", "").Trim();
}
}
protected void DataList2_ItemCommand(Object source, DataListCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView dataRowView = e.Item.DataItem as DataRowView;
string idcl = dataRowView["IdCL"].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("DivT");
theDivT.Attributes["class"] = theDivT.Attributes["class"].Replace("hidden", "").Trim();
}
}

Prasad RaveendranRank #320 · 7.8K reputationPosted Nov 17, 2024, 9:34 AM EST
When dealing with nested
ApproachDataListcontrols in ASP.NET Web Forms, handling events can be tricky due to the hierarchy. Since eachDataListis nested, you need to carefully retrieve the nested control to properly handle the event.Make sure you’re handling the
OnItemCommandevent for the secondDataList(DataList2), which can be done by capturing the event in the code-behind and retrieving the nestedDataList.Access the controls within each
DataListlevel by iterating through the items and finding the control.Here's an example of the code-behind logic for handling the
DataList2_ItemCommand:protected void DataList2_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == "Cladire") { // Find DivT in DataList2's current item var divT = (HtmlControl)e.Item.FindControl("DivT"); if (divT != null) { // Make DivT visible divT.Attributes["class"] = divT.Attributes["class"].Replace("hidden", ""); // Find DataList3 inside DivT var dataList3 = (DataList)divT.FindControl("DataList3"); if (dataList3 != null) { // Load data into DataList3 if needed // For example, you might bind data here based on the DataKey or other parameters int idCL = Convert.ToInt32(e.CommandArgument); dataList3.DataSource = GetCameraData(idCL); // Replace with your data source method dataList3.DataBind(); } } } }- DivT Visibility: Initially, the
- Data Binding for DataList3: Bind
Additional NotesDivTelement is hidden. When theCladirebutton is clicked, it triggersDataList2_ItemCommand, where you can makeDivTvisible by removing the"hidden"class.DataList3with data based on the command argument (IdCL). Use a method likeGetCameraDatato fetch the required data.OnItemCommand="DataList2_ItemCommand"is properly set onDataList2.DataList1if it’s interacting withDataList2.This approach should enable you to control and bind data dynamically to each nested
DataList.Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 11, 2024, 4:11 AM EST
The problem with the nested DataLists likely stems from the way the
OnItemCommandevents are being handled. In yourDataList2_ItemCommand, ensure that the command is being triggered correctly. You may want to check if theCommandNamefor the buttons inDataList2is set properly and that the event is wired up correctly in the markup.Here’s a refined approach to ensure that the commands are processed correctly:
Check CommandName: Ensure that the
CommandNamefor theLinkButtoninDataList2is unique and correctly referenced in the code-behind.Debugging: Add logging or breakpoints in the
DataList2_ItemCommandmethod to verify if it is being hit when you click the button.Data Binding: Ensure that the data binding for
DataList3is correctly set up. You can also check if theDataSourceis returning the expected results.protected void DataList2_ItemCommand(Object source, DataListCommandEventArgs e) { if (e.CommandName == "Cladire") // Ensure this matches the CommandName in your markup { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataRowView dataRowView = e.Item.DataItem as DataRowView; string idcl = dataRowView["IdCL"].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("DivT"); theDivT.Attributes["class"] = theDivT.Attributes["class"].Replace("hidden", "").Trim(); } } }By ensuring that the
CommandNameis correctly referenced and that the event is firing, you should be able to resolve the issues with the nested DataLists.Marius VasileRank #785 · 2.9K reputationPosted Nov 12, 2024, 12:27 PM EST
Thank you Jayraj,
It seems that onitemcommand from Datalist2 is not firing. Method "protected void DataList2_ItemCommand(Object source, DataListCommandEventArgs e)" is not activated therefore there should be another way to acces datalist2_itemcommand withinn Datalis1
Marius VasileRank #785 · 2.9K reputationPosted Nov 16, 2024, 6:29 AM EST
I still can't access item in a nested DataList. Any idea, please?
Marius VasileRank #785 · 2.9K reputationPosted Nov 16, 2024, 1:56 PM EST
Updated code behind
protected void DataList1_ItemCommand(Object source, DataListCommandEventArgs e) { if (e.CommandName == "Obiectiv") { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { int idob = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString()); DataList secondDataList = e.Item.FindControl("DataList2") as DataList; List prm = new List(); prm.Add(new SqlParameter("@IdOB", idob)); secondDataList.DataSource = GetDataTable("SELECT DISTINCT IdCL,Cladire FROM tblCladire WHERE IdOB= @IdOB;", prm); secondDataList.DataBind(); HtmlControl theDivS = (HtmlControl)e.Item.FindControl("DivS"); theDivS.Attributes["class"] = theDivS.Attributes["class"].Replace("hidden", "").Trim(); } } } protected void DataList2_ItemCommand(Object source, DataListCommandEventArgs e) { if (e.CommandName == "Cladire") { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataList secondDataList = e.Item.FindControl("DataList2") as DataList; int idcl = Convert.ToInt32(secondDataList.DataKeys[e.Item.ItemIndex].ToString()); DataList thirdDataList = e.Item.FindControl("DataList3") as DataList; List prm = new List { new SqlParameter("@IdCL", idcl) }; thirdDataList.DataSource = GetDataTable("SELECT DISTINCT IdCA,Camera FROM tblCamera WHERE IdCL =@IdCL;", prm); thirdDataList.DataBind(); HtmlControl theDivT = (HtmlControl)e.Item.FindControl("DivT"); theDivT.Attributes["class"] = theDivT.Attributes["class"].Replace("hidden", "").Trim(); } } }