用repeater嵌套CheckBox+CheckBoxList实现树型选择菜单

页面:
<HTML>
 <body>
  <form id="Form1" runat="server">
   <asp:repeater id="parentRepeater" runat="server">
    <itemtemplate>
     <b>
      <asp:CheckBox id="CheckBoxRole" Text='<%# DataBinder.Eval(Container.DataItem,"RoleId") %>' runat="server" AutoPostBack="True" OnCheckedChanged="CheckBoxRole_CheckedChanged">
      </asp:CheckBox>
      <asp:CheckBoxList id="CheckBoxListUserId" Runat="server" DataValueField="RoleId"></asp:CheckBoxList>
     </b>
     <br>
    </itemtemplate>
   </asp:repeater>
  </form>
 </body>
</HTML>

后台代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace center
{
 /// <summary>
 /// NestedRepeater 的摘要说明。
 /// </summary>
 public class NestedRepeater : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Repeater parentRepeater;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!Page.IsPostBack)
   {
    // 在此处放置用户代码以初始化页面
    // 为Authors表创建 Connection 和 DataAdapter
    string cnnString = @"server=azure;database=CnForums;uid=sa;pwd=;";
    SqlConnection cnn = new SqlConnection(cnnString);
    SqlDataAdapter cmd1 = new SqlDataAdapter("select * from aspnet_Roles",cnn);
  
    //创建填充 DataSet.
    DataSet ds = new DataSet();
    cmd1.Fill(ds,"Roles");
  
    // 为Titles表创建 DataAdapter
    SqlDataAdapter cmd2 = new SqlDataAdapter("select * from aspnet_UsersInRoles",cnn);
    cmd2.Fill(ds,"Users");
  
    // 创建 Authors 表和 Titles 表之间的关系.
    ds.Relations.Add("myrelation",
    ds.Tables["Roles"].Columns["RoleId"],
    ds.Tables["Users"].Columns["RoleId"]);
  
    // 绑定Authors到父Repeater
    parentRepeater.DataSource = ds.Tables["Roles"];
    Page.DataBind();

    cnn.Close();
    cnn.Dispose();
   }
  }

Web 窗体设计器生成的代码

  public void CheckBoxRole_CheckedChanged(object sender, System.EventArgs e)
  {
   for(int i=0;i<this.parentRepeater.Items.Count;i++)
   {
    CheckBox cb = (CheckBox)parentRepeater.Items[i].FindControl("CheckBoxRole");
    CheckBoxList cbl = (CheckBoxList)parentRepeater.Items[i].FindControl("CheckBoxListUserId");
    if (cb.Checked==true)
    {
     cbl.Visible=true;
    }
    if (cb.Checked==false)
    {
     cbl.Visible=false;
    }
   }
  }

  private void parentRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
  {
   string cnnString = @"server=azure;database=CnForums;uid=sa;pwd=;";
   SqlConnection cnn = new SqlConnection(cnnString);
   SqlDataAdapter cmd1 = new SqlDataAdapter("select * from aspnet_Roles",cnn);
   //创建填充 DataSet.
   DataSet ds = new DataSet();
   cmd1.Fill(ds,"Roles");
   //绑定checkboxlist
   CheckBoxList cb=(CheckBoxList)e.Item.FindControl("CheckBoxListUserId");
   DataRowView row=(DataRowView)e.Item.DataItem;
   string root=row["RoleId"].ToString();
   cb.DataSource=ds;
   cb.DataTextField="RoleId";
   cb.DataValueField="RoleId";
   cb.DataBind();
  }
 }
}

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值