datalist嵌套datalist实现内层datalist编辑(更新、取消、删除)

围观一下思路:

〈1〉先绑定d1和d2(d1外层d2是内层)

       1>在page_load中加载d1的绑定数据

       2>在d1_ItemDataBound中根据d1的数据对应绑定d2数据

           A.先找出d2控件:e.Item.FindControl("d2")

           B.找到外层的数据项DataRowView drv = (DataRowView)e.Item.DataItem;然后找到外层主键object id = drv["id"];          

           C.根据外层主键绑定内层数据

〈2〉外层编辑、取消、更新就不具体说了:d1_EditCommand、d1_CancelCommand、d1_UpdateCommand

〈3〉在内层d2_ItemCommand事件中实现内层控件d2的编辑、取消、更新、删除

       1>得到内层控件id->d2:(DataList)e.Item.Parent;

       2>得到内层正要编辑的主键id即small_id:(HiddenField)e.Item.FindControl("h2")).Value;

       3>根据内层主键id得到外层主键id即big_id:select big_id from table2 where small_id=....

       4>实现编辑、更新、取消、删除(根据e.CommandName == ...进行判断)

数据库两个表:

大文件表:

小文件表:

前台代码:

<asp:DataList ID="d1" runat="server" OnItemDataBound="d1_ItemDataBound" OnEditCommand="d1_EditCommand"
        OnCancelCommand="d1_CancelCommand" OnUpdateCommand="d1_UpdateCommand">
        <ItemTemplate>
            <asp:HiddenField ID="h1" Value='<%# Eval("id") %>' runat="server" />
            <span style="font-size: large; color: Red">
                <%# Eval("big_sFile") %></span><asp:Button ID="b1" runat="server" Text="编辑" CommandName="edit" /><br />
            <asp:DataList ID="d2" runat="server" OnItemCommand="d2_ItemCommand">
                <ItemTemplate>
                    <asp:HiddenField ID="h2" Value='<%# Eval("id") %>' runat="server" />
                    <%# Eval("small_sFile") %><asp:Button ID="b2" runat="server" Text="编辑" CommandName="Edit" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:HiddenField ID="h2" Value='<%# Eval("id") %>' runat="server" />
                    <asp:TextBox ID="t2" Text='<%# Eval("small_sFile") %>' runat="server"></asp:TextBox><asp:Button
                        ID="bupdate" runat="server" CommandName="Update" Text="更新" /><asp:Button ID="bcanel"
                            runat="server" Text="取消" CommandName="Cancel" />
                </EditItemTemplate>
            </asp:DataList>
        </ItemTemplate>
        <EditItemTemplate>
            <asp:HiddenField ID="h1" Value='<%# Eval("id") %>' runat="server" />
            <asp:TextBox ID="t1" Text='<%# Eval("big_sFile") %>' runat="server"></asp:TextBox><asp:Button
                ID="bupdate" runat="server" CommandName="Update" Text="更新" /><asp:Button ID="bcanel"
                    runat="server" Text="取消" CommandName="Cancel" />
        </EditItemTemplate>
    </asp:DataList>

 后台.cs代码

DataAccess d = new DataAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    public void bind()
    {
        d1.DataSource = d.getDataSet("select * from tb_big_img", false);
        d1.DataBind();
    }
    protected void d1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataList list = (DataList)e.Item.FindControl("d2");
            DataRowView drv = (DataRowView)e.Item.DataItem;
            object id = drv["id"];
            if (id != null)
            {
                list.DataSource = d.getDataSet("select * from tb_small_img where bid='" + id.ToString() + "'", false);
                list.DataBind();
            }
        }
    }
    protected void d1_EditCommand(object source, DataListCommandEventArgs e)
    {
        d1.EditItemIndex = e.Item.ItemIndex;
        bind();
    }
    protected void d1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        d1.EditItemIndex = -1;
        bind();
    }
    protected void d1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        string id = ((HiddenField)e.Item.FindControl("h1")).Value;
        string big_img = ((TextBox)e.Item.FindControl("t1")).Text;
        d.getCount("update tb_big_img set big_sFile='" + big_img + "' where id='" + id + "'", false);
        d1.EditItemIndex = -1;
        bind();
    }
    protected void d2_ItemCommand(object source, DataListCommandEventArgs e)
    {
        DataList d2 = (DataList)e.Item.Parent;//得到内层datalist的id即d2
        string small_id = ((HiddenField)e.Item.FindControl("h2")).Value;//得到内层正要编辑的id即small_id
        string big_id = d.getSingle("select bid from tb_small_img where id='" + small_id + "'", false).ToString();//根据内层id得到外层id即big_id
        if (e.CommandName == "Edit")
        {
            d2.EditItemIndex = e.Item.ItemIndex;
        }
        else if (e.CommandName == "Cancel")
        {
            d2.EditItemIndex = -1;
        }
        else if (e.CommandName == "Update")
        {
            string sFile = ((TextBox)e.Item.FindControl("t2")).Text;
            d.getCount("update tb_small_img set small_sFile='" + sFile + "' where id='" + small_id + "'", false);
            d2.EditItemIndex = -1;
        }
        d2.DataSource = d.getDataSet("select * from tb_small_img where bid='" + big_id + "'", false);
        d2.DataBind();
    }

 注意:有待思考:在内层d2_ItemCommand事件中如何直接获取外层对应行的主键

          想法:DataList d1 = (DataList)e.Item.Parent.Parent.Parent;????????

          请大家指正……

posted @ 2012-07-24 10:07  aspneteye  阅读(1721)  评论(0编辑  收藏  举报