PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > ASP.NET教程 > 正文

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

    作者:admin来源:B5教程网浏览:时间:2020-09-30 00:07:50我要评论
    导读:用repeater嵌套CheckBox+CheckBoxList实现树型选择菜单
     
    复制代码代码如下:
    1. <HTML>
    2.  <body>
    3.   <form id="Form1" runat="server">
    4.    <asp:repeater id="parentRepeater" runat="server">
    5.     <itemtemplate>
    6.      <b>
    7.       <asp:CheckBox id="CheckBoxRole" Text='<%# DataBinder.Eval(Container.DataItem,"RoleId") %>' runat="server" AutoPostBack="True" OnCheckedChanged="CheckBoxRole_CheckedChanged">
    8.       </asp:CheckBox>
    9.       <asp:CheckBoxList id="CheckBoxListUserId" Runat="server" DataValueField="RoleId"></asp:CheckBoxList>
    10.      </b>
    11.      <br>
    12.     </itemtemplate>
    13.    </asp:repeater>
    14.   </form>
    15.  </body>
    16. </HTML>
    17.  
    18. 后台代码
    19. using System;
    20. using System.Collections;
    21. using System.ComponentModel;
    22. using System.Data;
    23. using System.Drawing;
    24. using System.Web;
    25. using System.Web.SessionState;
    26. using System.Web.UI;
    27. using System.Web.UI.WebControls;
    28. using System.Web.UI.HtmlControls;
    29. using System.Data.SqlClient;
    30.  
    31. namespace center
    32. {
    33.  ///
    34.  /// NestedRepeater 的摘要说明。
    35.  ///
    36.  public class NestedRepeater : System.Web.UI.Page
    37.  {
    38.   protected System.Web.UI.WebControls.Repeater parentRepeater;
    39.  
    40.   private void Page_Load(object sender, System.EventArgs e)
    41.   {
    42.    if(!Page.IsPostBack)
    43.    {
    44.     // 在此处放置用户代码以初始化页面
    45.     // 为Authors表创建 Connection 和 DataAdapter
    46.     string cnnString = @"server=azure;database=CnForums;uid=sa;pwd=;";
    47.     SqlConnection cnn = new SqlConnection(cnnString);
    48.     SqlDataAdapter cmd1 = new SqlDataAdapter("select * from aspnet_Roles",cnn);
    49.   
    50.     //创建填充 DataSet.
    51.     DataSet ds = new DataSet();
    52.     cmd1.Fill(ds,"Roles");
    53.   
    54.     // 为Titles表创建 DataAdapter
    55.     SqlDataAdapter cmd2 = new SqlDataAdapter("select * from aspnet_UsersInRoles",cnn);
    56.     cmd2.Fill(ds,"Users");
    57.   
    58.     // 创建 Authors 表和 Titles 表之间的关系.
    59.     ds.Relations.Add("myrelation",
    60.     ds.Tables["Roles"].Columns["RoleId"],
    61.     ds.Tables["Users"].Columns["RoleId"]);
    62.   
    63.     // 绑定Authors到父Repeater
    64.     parentRepeater.DataSource = ds.Tables["Roles"];
    65.     Page.DataBind();
    66.  
    67.     cnn.Close();
    68.     cnn.Dispose();
    69.    }
    70.   }
    71.  
    72. Web 窗体设计器生成的代码
    73.  
    74.   public void CheckBoxRole_CheckedChanged(object sender, System.EventArgs e)
    75.   {
    76.    for(int i=0;i<this.parentRepeater.Items.Count;i++)
    77.    {
    78.     CheckBox cb = (CheckBox)parentRepeater.Items[i].FindControl("CheckBoxRole");
    79.     CheckBoxList cbl = (CheckBoxList)parentRepeater.Items[i].FindControl("CheckBoxListUserId");
    80.     if (cb.Checked==true)
    81.     {
    82.      cbl.Visible=true;
    83.     }
    84.     if (cb.Checked==false)
    85.     {
    86.      cbl.Visible=false;
    87.     }
    88.    }
    89.   }
    90.  
    91.   private void parentRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
    92.   {
    93.    string cnnString = @"server=azure;database=CnForums;uid=sa;pwd=;";
    94.    SqlConnection cnn = new SqlConnection(cnnString);
    95.    SqlDataAdapter cmd1 = new SqlDataAdapter("select * from aspnet_Roles",cnn);
    96.    //创建填充 DataSet.
    97.    DataSet ds = new DataSet();
    98.    cmd1.Fill(ds,"Roles");
    99.    //绑定checkboxlist
    100.    CheckBoxList cb=(CheckBoxList)e.Item.FindControl("CheckBoxListUserId");
    101.    DataRowView row=(DataRowView)e.Item.DataItem;
    102.    string root=row["RoleId"].ToString();
    103.    cb.DataSource=ds;
    104.    cb.DataTextField="RoleId";
    105.    cb.DataValueField="RoleId";
    106.    cb.DataBind();
    107.   }
    108.  }
    109. }
    110.  

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-11-194-1.html