1. 程式人生 > >用repeater巢狀CheckBox+CheckBoxList實現樹型選擇選單

用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();
  }
 }
}