Repeater嵌套,增删改查综合运用

本文介绍了一个使用ASP.NET中嵌套Repeater控件的示例,演示了如何通过Repeater控件实现对类别及其子模块进行增删改操作,并提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
...{
    private const string connectionStr = @"Data Source=.SQLEXPRESS;AttachDbFilename=D:QianTaoApp_Datadb.mdf;Integrated Security=True;User Instance=True";

    protected void Page_Load(object sender, EventArgs e)
    ...{
        if(!IsPostBack)
        ...{
            SetBind();
        }
    }

    protected void rptClass_ItemCommand(object source, RepeaterCommandEventArgs e)
    ...{
        //判断是否添加类别
        if (e.CommandName == "AddClass")
        ...{
            TextBox tb = e.Item.FindControl("txtClassName") as TextBox;
            using(SqlConnection conn = new SqlConnection(connectionStr))
            ...{
                conn.Open();
                using(SqlCommand cmd = new SqlCommand("insert into tb_Class(className) values(@className)", conn))
                ...{
                    cmd.Parameters.AddWithValue("@className", tb.Text);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
        //判断是否删除类别
        if (e.CommandName == "DelClass")
        ...{
            using (SqlConnection conn = new SqlConnection(connectionStr))
            ...{
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("delete from tb_Module where classId=@classId;delete from tb_Class where id=@classId", conn))
                ...{
                    cmd.Parameters.AddWithValue("@classId", e.CommandArgument);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
        //判断是否修改类别
        if (e.CommandName == "UpdateClass")
        ...{
            TextBox tb = e.Item.FindControl("txtClassName") as TextBox;
            using (SqlConnection conn = new SqlConnection(connectionStr))
            ...{
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("update tb_Class set className=@className where id=@id", conn))
                ...{
                    cmd.Parameters.AddWithValue("@className", tb.Text);
                    cmd.Parameters.AddWithValue("@id", e.CommandArgument);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
        //判断是否添加模块
        if (e.CommandName == "AddModule")
        ...{
            TextBox tb = e.Item.FindControl("txtModuleName") as TextBox;
            using (SqlConnection conn = new SqlConnection(connectionStr))
            ...{
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("insert into tb_Module(classId,moduleName)values(@classId,@moduleName)", conn))
                ...{
                    cmd.Parameters.AddWithValue("@moduleName", tb.Text);
                    cmd.Parameters.AddWithValue("@classId", e.CommandArgument);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
    }

    /**//**//**//// <summary>
    /// 嵌套repeater的ItemCommand事件
    /// </summary>
    protected void rptModule_ItemCommand(object source, RepeaterCommandEventArgs e)
    ...{
        //判断是否删除模块
        if (e.CommandName == "DelModule")
        ...{
            using (SqlConnection conn = new SqlConnection(connectionStr))
            ...{
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("delete from tb_Module where id=@Id;", conn))
                ...{
                    cmd.Parameters.AddWithValue("@Id", e.CommandArgument);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
        //判断是否修改模块
        if (e.CommandName == "UpdateModule")
        ...{
            TextBox tb = e.Item.FindControl("txtModuleName") as TextBox;
            using (SqlConnection conn = new SqlConnection(connectionStr))
            ...{
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("update tb_Module set moduleName=@Name where id=@id", conn))
                ...{
                    cmd.Parameters.AddWithValue("@Name", tb.Text);
                    cmd.Parameters.AddWithValue("@id", e.CommandArgument);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
    }

    /**//**//**//// <summary>
    /// 绑定Repeater
    /// </summary>
    private void SetBind()
    ...{
        DataSet ds = new DataSet();
        using(SqlConnection conn = new SqlConnection(connectionStr))
        ...{
            SqlDataAdapter adapter =
                new SqlDataAdapter("select * from tb_Class",conn);
            adapter.Fill(ds);
        }
        rptClass.DataSource = ds;
        rptClass.DataBind();
    }

    protected void rptClass_ItemDataBound(object sender, RepeaterItemEventArgs e)
    ...{       
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        ...{
            Repeater rpt = e.Item.FindControl("rptModule") as Repeater;
            //找到分类Repeater关联的数据项
            DataRowView rowv = (DataRowView)e.Item.DataItem;
            //提取分类ID
            int id = (int)rowv["id"];
            DataSet ds = new DataSet();
            using (SqlConnection conn = new SqlConnection(connectionStr))
            ...{
                SqlDataAdapter adapter =
                    new SqlDataAdapter("select * from tb_Module where classId="+ id +"", conn);
                adapter.Fill(ds);
            }

            rpt.DataSource = ds;
            rpt.DataBind();
        }

    }
}


 ------这是页面------

 

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>嵌套RepeaterDEMO</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="rptClass" runat="server" OnItemCommand="rptClass_ItemCommand" OnItemDataBound="rptClass_ItemDataBound">
                <HeaderTemplate>
                    <table>
                        <tr style="background-color:yellow">
                            <td>
                                添加类别:
                                <asp:TextBox ID="txtClassName" runat="server">
                                </asp:TextBox>
                                <asp:Button ID="btnAddClass" runat="server" CommandName="AddClass" Text="添加" />
                            </td>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr style="background-color:#CCCCCC">
                        <td>
                            <asp:Button ID="btnDelClass" CommandArgument='<%# Eval("id") %>' CommandName="DelClass" runat="server" Text="删除类别" />
                            <asp:TextBox ID="txtClassName" runat="server" Text='<%# Eval("className") %>'>                       
                            </asp:TextBox>
                            <asp:Button ID="btnUpdateClass" CommandArgument='<%# Eval("id") %>' CommandName="UpdateClass" runat="server" Text="修改类别" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            添加模块:
                            <asp:TextBox ID="txtModuleName" runat="server">
                            </asp:TextBox>
                            <asp:Button ID="btnAddModule" runat="server" CommandName="AddModule" CommandArgument='<%# Eval("id") %>' Text="添加" />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Repeater ID="rptModule" runat="server"
                             OnItemCommand='rptModule_ItemCommand'>
                             <ItemTemplate>
                                <tr>
                                    <td>
                                        &nbsp;&nbsp;&nbsp;&nbsp;-----------------
                                        <asp:Button ID="btnDeleteModule" CommandArgument='<%# Eval("id") %>' CommandName="DelModule" runat="server" Text="删除模块" />
                                        <asp:TextBox ID="txtModuleName" Text='<%# Eval("moduleName") %>' runat="server"></asp:TextBox>
                                        <asp:Button ID="btnUpdateModule" CommandArgument='<%# Eval("id") %>' CommandName="UpdateModule" runat="server" Text="修改模块" />
                                    </td>
                                </tr>
                             </ItemTemplate>
                            </asp:Repeater>    
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>
</html>
文章出处:http://www.diybl.com/course/4_webprogram/asp.net/netjs/2007112/82403.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值