无法通过编辑,更新命令来更新嵌套数据列表控件的项 [英] can't update nested datalist control's items by edit,update commands

查看:46
本文介绍了无法通过编辑,更新命令来更新嵌套数据列表控件的项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的专家,

我正在使用两个数据列表控件,一个在另一个控件下.

我可以使用编辑轻松地编辑和更新父数据列表项,以更新命令按钮和命令处理程序.
但是对于子数据列表控件,我无法使用命令按钮和子数据列表处理程序进行编辑和更新.

我一直在努力.

我用google寻找解决方案,但还没有解决方案.
请提供给我用于使用命令按钮编辑和更新父级和子级数据列表控件的链接.

如果需要,我可以粘贴我使用过的所有代码.

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.

推荐答案

要明确,我在此处发布了必要的代码-
这是我的两个数据列表所在的aspx页面代码-

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 />       



这是.cs文件的代码-



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; //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!


这篇关于无法通过编辑,更新命令来更新嵌套数据列表控件的项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆