to be clear here i posted necessary codes -
here is the aspx page code where my two datalist reside -
<asp:DataList ID="DataList1" DataKeyField="CommentId" CssClass="MSComItem" runat="server" OnItemDataBound="DataList1_ItemDataBound" OnItemCommand="DataList1_ItemCommand" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" OnEditCommand="DataList1_EditCommand" OnDeleteCommand="DataList1_DeleteCommand" OnCancelCommand="DataList1_CancelCommand" OnUpdateCommand="DataList1_UpdateCommand">
<itemtemplate>
<div style=" margin-removed110px;">
<asp:LinkButton ID="Approve" CommandName="select" runat="server" >Approve
<asp:LinkButton ID="Edit" Text="Edit" CommandName="Edit" runat="server" />
<asp:LinkButton ID="delete" Text="Delete" CommandName="Delete" runat="server" />
</div>
<div class="commentdiv" style="float:left"><asp:TextBox ID="Comments" Enabled="false" CssClass="textbox" BorderColor="White" BorderWidth="1px" TextMode="MultiLine" runat="server" Text='<%# Bind("Comment")%>' ></div>
<asp:DataList ID="DataList2" DataKeyField="CommentId" CssClass="MSComItem" runat="server" OnEditCommand="DataList2_EditCommand" OnDeleteCommand="DataList2_DeleteCommand" OnCancelCommand="DataList2_CancelCommand" OnUpdateCommand="DataList2_UpdateCommand">
<itemtemplate>
<div style=" margin-removed110px;">
<asp:LinkButton ID="Approve2" CommandName="select" runat="server" >Approve
<asp:LinkButton ID="Edit2" Text="Edit" CommandName="Edit" runat="server" />
<asp:LinkButton ID="delete2" Text="Delete" CommandName="Delete" runat="server" />
</div>
<div class="commentdiv" style="float:left"><asp:TextBox ID="Comments2" Enabled="false" CssClass="textbox" BorderColor="White" BorderWidth="1px" TextMode="MultiLine" runat="server" Text='<%# Bind("Comment")%>' ></div>
</itemtemplate>
<edititemtemplate>
<div style=" margin-removed110px;">
<asp:LinkButton ID="Button11" runat="server" CommandName="Update" Text="Update" />
<asp:LinkButton ID="Button22" runat="server" CommandName="Cancel" Text="Cancel" />
</div>
<div class="commentdiv" style="float:left"><asp:TextBox ID="Comments2" Enabled="true" CssClass="textbox" BorderColor="White" BorderWidth="1px" TextMode="MultiLine" runat="server" Text='<%# Bind("Comment")%>' ></div>
</edititemtemplate>
<itemstyle />
</itemtemplate>
<edititemtemplate>
(did not post here)
</edititemtemplate>
<itemstyle />
Here are the .cs file code -
protected void DataList1_ItemDataBound(object source, DataListItemEventArgs e)
{
parentcid = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
MS_Comments_Controller control = new MS_Comments_Controller();
List<comments> commentsList = new List<comments>();
commentsList = control.MS_Comments_LoadChildComents(parentcid);
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
((DataList)e.Item.FindControl("DataList2")).DataSource = commentsList;
((DataList)e.Item.FindControl("DataList2")).DataBind();
}
}
protected void DataList2_EditCommand(object source, DataListCommandEventArgs e)
{
int i;
for (i = 0; i < DataList1.Items.Count; i++)
{
DataList dl1 = (DataList)DataList1.Items[i].FindControl("DataList2");
if (Int32.Parse(dl1.Items.Count.ToString()) > 0)
break;
}
DataList second = ((DataList)DataList1.Items[i].FindControl("DataList2"));
second.EditItemIndex=e.Item.ItemIndex;
parentcid = Convert.ToInt32(DataList1.DataKeys[i].ToString());
List<comments> commentsList = new List<comments>();
MS_Comments_Controller control = new MS_Comments_Controller();
commentsList = control.MS_Comments_LoadChildComents(parentcid);
second.DataSource = commentsList;
second.DataBind();
}
protected void DataList2_UpdateCommand(object source, DataListCommandEventArgs e)
{
Int32 cid;
String comment;
int i;
for (i = 0; i < DataList1.Items.Count; i++)
{
DataList dl1 = (DataList)DataList1.Items[i].FindControl("DataList2");
if (Int32.Parse(dl1.Items.Count.ToString()) > 0)
break;
}
DataList second = (DataList)DataList1.Items[i].FindControl("DataList2");
cid = Int32.Parse(second.DataKeys[e.Item.ItemIndex].ToString());
TextBox box=(TextBox)second.Items[e.Item.ItemIndex].FindControl("Comments2");
comment = box.Text;
MS_Comments_Controller control = new MS_Comments_Controller();
control.MS_Comments_UpdateComment(cid, comment);
second.EditItemIndex = -1;
parentcid = Convert.ToInt32(DataList1.DataKeys[i].ToString());
List<comments> commentsList = new List<comments>();
MS_Comments_Controller control2 = new MS_Comments_Controller();
commentsList = control.MS_Comments_LoadChildComents(parentcid);
second.DataSource = commentsList;
second.DataBind();
}</comments></comments></comments></comments></comments></comments>
now when i load my page i see both datalist populated.but the edit button not works for every child datalist items.please help!