{
    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)
    [转帖]repeater控件常用方法{
        
if(!IsPostBack)
        [转帖]repeater控件常用方法{
            SetBind();
        }
    }

    
protected void rptClass_ItemCommand(object source, RepeaterCommandEventArgs e)
    [转帖]repeater控件常用方法{
        
//判断是否添加类别
        if (e.CommandName == "AddClass")
        [转帖]repeater控件常用方法{
            TextBox tb 
= e.Item.FindControl("txtClassName"as TextBox;
            
using(SqlConnection conn = new SqlConnection(connectionStr))
            [转帖]repeater控件常用方法{
                conn.Open();
                
using(SqlCommand cmd = new SqlCommand("insert into tb_Class(className) values(@className)", conn))
                [转帖]repeater控件常用方法{
                    cmd.Parameters.AddWithValue(
"@className", tb.Text);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
        
//判断是否删除类别
        if (e.CommandName == "DelClass")
        [转帖]repeater控件常用方法{
            
using (SqlConnection conn = new SqlConnection(connectionStr))
            [转帖]repeater控件常用方法{
                conn.Open();
                
using (SqlCommand cmd = new SqlCommand("delete from tb_Module where classId=@classId;delete from tb_Class where id=@classId", conn))
                [转帖]repeater控件常用方法{
                    cmd.Parameters.AddWithValue(
"@classId", e.CommandArgument);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
        
//判断是否修改类别
        if (e.CommandName == "UpdateClass")
        [转帖]repeater控件常用方法{
            TextBox tb 
= e.Item.FindControl("txtClassName"as TextBox;
            
using (SqlConnection conn = new SqlConnection(connectionStr))
            [转帖]repeater控件常用方法{
                conn.Open();
                
using (SqlCommand cmd = new SqlCommand("update tb_Class set className=@className where id=@id", conn))
                [转帖]repeater控件常用方法{
                    cmd.Parameters.AddWithValue(
"@className", tb.Text);
                    cmd.Parameters.AddWithValue(
"@id", e.CommandArgument);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
        
//判断是否添加模块
        if (e.CommandName == "AddModule")
        [转帖]repeater控件常用方法{
            TextBox tb 
= e.Item.FindControl("txtModuleName"as TextBox;
            
using (SqlConnection conn = new SqlConnection(connectionStr))
            [转帖]repeater控件常用方法{
                conn.Open();
                
using (SqlCommand cmd = new SqlCommand("insert into tb_Module(classId,moduleName)values(@classId,@moduleName)", conn))
                [转帖]repeater控件常用方法{
                    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)
    [转帖]repeater控件常用方法{
        
//判断是否删除模块
        if (e.CommandName == "DelModule")
        [转帖]repeater控件常用方法{
            
using (SqlConnection conn = new SqlConnection(connectionStr))
            [转帖]repeater控件常用方法{
                conn.Open();
                
using (SqlCommand cmd = new SqlCommand("delete from tb_Module where id=@Id;", conn))
                [转帖]repeater控件常用方法{
                    cmd.Parameters.AddWithValue(
"@Id", e.CommandArgument);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
        
//判断是否修改模块
        if (e.CommandName == "UpdateModule")
        [转帖]repeater控件常用方法{
            TextBox tb 
= e.Item.FindControl("txtModuleName"as TextBox;
            
using (SqlConnection conn = new SqlConnection(connectionStr))
            [转帖]repeater控件常用方法{
                conn.Open();
                
using (SqlCommand cmd = new SqlCommand("update tb_Module set moduleName=@Name where id=@id", conn))
                [转帖]repeater控件常用方法{
                    cmd.Parameters.AddWithValue(
"@Name", tb.Text);
                    cmd.Parameters.AddWithValue(
"@id", e.CommandArgument);
                    cmd.ExecuteNonQuery();
                    SetBind();
                }
            }
        }
    }

    
/**//**//**//// <summary>
    
/// 绑定Repeater
    
/// </summary>
    private void SetBind()
    [转帖]repeater控件常用方法{
        DataSet ds 
= new DataSet();
        
using(SqlConnection conn = new SqlConnection(connectionStr))
        [转帖]repeater控件常用方法{
            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)
    [转帖]repeater控件常用方法{        
        
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        [转帖]repeater控件常用方法{
            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))
            [转帖]repeater控件常用方法{
                SqlDataAdapter adapter 
=
                    
new SqlDataAdapter("select * from tb_Module where classId="+ id +"", conn);
                adapter.Fill(ds);
            }

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

    }
}

前台页面

 


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

相关文章:

  • 2022-12-23
  • 2021-10-21
  • 2021-09-18
  • 2021-09-30
  • 2022-12-23
  • 2021-05-11
  • 2022-12-23
  • 2021-06-11
猜你喜欢
  • 2021-05-18
  • 2021-11-10
  • 2021-05-30
  • 2022-02-03
  • 2021-06-11
  • 2021-09-01
相关资源
相似解决方案