Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear experts,

I am using two datalist controls, one under another.

I can easily edit and update parent datalist items, using edit, to update command buttons and command handlers.
But for child datalist control I can't edit and update using command buttons and handlers for child datalist.

I have been struggling.

I used google for finding solution but no solution got yet.
Please provide me links for edit and updating both parent and child datalist controls using command buttons.

If needed,Ii can paste all my codes I used.
Posted
Updated 16-Apr-11 3:32am
v2
Comments
tulasiram3975 16-Apr-11 7:34am    
You Are Using Two Separate Datalists if Yes Check In Page_Load Dont Bind Data to data list control At PostBack
mridul samadder 16-Apr-11 8:17am    
i bind the first datalist under if (!Page.IsPostBack) condition in page load.
Dalek Dave 16-Apr-11 9:32am    
Edited for Grammar and Readability.

1 solution

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 -

C#
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; //without it not works

            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;//pages;
            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!
 
Share this answer
 
v3
Comments
Dalek Dave 16-Apr-11 9:35am    
Edited for Code Blocks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900