ASP.NET Tutorial/ASP.net Controls/CheckBoxList

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Adding ListItems Declararatively

   <source lang="csharp">

<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>CheckBoxList Control</title>

</head> <body>

   <form id="form1" runat="server">

CheckBoxList Control

Adding ListItems Declararatively

    <asp:CheckBoxList ID="cblItems" runat="server" AutoPostBack="True" CellPadding="5" CellSpacing="10" RepeatColumns="3" RepeatDirection="Horizontal" RepeatLayout="Flow" TextAlign="Left">
        <asp:ListItem> Item 1 </asp:ListItem>   
        <asp:ListItem> Item 2 </asp:ListItem>
        <asp:ListItem> Item 3 </asp:ListItem>
        <asp:ListItem> Item 5 </asp:ListItem>
        <asp:ListItem> Item 6 </asp:ListItem>
    </asp:CheckBoxList>
   
   </form>

</body> </html></source>


Adding ListItems from an Array

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>CheckBoxList Control</title>

</head> <body>

   <form id="form1" runat="server">

CheckBoxList Control

Adding ListItems from an Array

    <asp:CheckBoxList ID="cblGenre" runat="server" AutoPostBack="True" CellPadding="5" CellSpacing="10" RepeatColumns="3" OnInit="cblGenre_Init">
        <asp:ListItem> Item 1 </asp:ListItem>   
        <asp:ListItem> Item 2 </asp:ListItem>
        <asp:ListItem> Item 3 </asp:ListItem>
        <asp:ListItem> Item 4 </asp:ListItem>
        <asp:ListItem> Item 5 </asp:ListItem>
        <asp:ListItem> Item 6 </asp:ListItem>
    </asp:CheckBoxList>
   
   </form>

</body> </html> File: Default.aspx.cs using System; using System.Data; 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_aspx : System.Web.UI.Page {

  protected void cblGenre_Init(object sender, EventArgs e)
  {
    string[] Genre = { "A", "B", "C", "D", "E" };
    for (int i = 0; i < Genre.Length; i++)
    {
      this.cblGenre.Items.Add(new ListItem(Genre[i]));
    }
  }
}</source>
   
  

Adding ListItems from an Array Using DataBinding

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>CheckBoxList Control</title>

</head> <body>

   <form id="form1" runat="server">

CheckBoxList Control

Adding ListItems from an Array Using DataBinding

    <asp:CheckBoxList ID="cblGenre" runat="server" AutoPostBack="True" CellPadding="5" CellSpacing="10" RepeatColumns="3" OnInit="cblGenre_Init" >
        <asp:ListItem value="1"> Item 1 </asp:ListItem>
        <asp:ListItem text="Item 2" value="2"></asp:ListItem>
        <asp:ListItem text="Item 3"/>
        <asp:ListItem text="Item 4"> Inner Item 4 </asp:ListItem>
        <asp:ListItem value="5"></asp:ListItem>
        <asp:ListItem> Item 6 </asp:ListItem>
    </asp:CheckBoxList>
   
   </form>

</body> </html> File: Default.aspx.cs using System; using System.Data; 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_aspx : System.Web.UI.Page {

  protected void cblGenre_Init(object sender, EventArgs e)
  {
    string[] Genre = { "SciFi", "Novels", "Computers", "History", "Religion" };
    cblGenre.DataSource = Genre;
    cblGenre.DataBind();
  }
}</source>
   
  

Adding ListItems from an Array With a Value

   <source lang="csharp">

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default_aspx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>CheckBoxList Control</title>

</head> <body>

   <form id="form1" runat="server">

CheckBoxList Control

Adding ListItems from an Array With a Value

    <asp:CheckBoxList ID="cblGenre" runat="server" AutoPostBack="True" CellPadding="5" CellSpacing="10" RepeatColumns="3" OnInit="cblGenre_Init">
        <asp:ListItem value="1"> Item 1 </asp:ListItem>
        <asp:ListItem text="Item 2" value="2"></asp:ListItem>
        <asp:ListItem text="Item 3"/>
        <asp:ListItem text="Item 4"> Inner Item 4 </asp:ListItem>
        <asp:ListItem value="5"></asp:ListItem>
        <asp:ListItem> Item 6 </asp:ListItem>
    </asp:CheckBoxList>
   
   </form>

</body> </html> File: Default.aspx.cs using System; using System.Data; 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_aspx : System.Web.UI.Page {

  protected void cblGenre_Init(object sender, EventArgs e)
  {
    string[] Genre = { "SciFi", "Novels", "Computers", "History", "Religion" };
    string[] Code = { "sf", "nvl", "cmp", "his", "rel" };
    for (int i = 0; i < Genre.Length; i++)
    {
      this.cblGenre.Items.Add(new ListItem(Genre[i], Code[i]));
    }
    }

}</source>


Fill data into asp:CheckBoxList and get selected items (C#)

   <source lang="csharp">

File: Default.aspx <%@ Page language="c#" Inherits="CheckBoxTest" CodeFile="Default.aspx.cs" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"

"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server">

 <title>CheckBoxTest</title>

</head> <body>

 <form ID="Form1" runat="server">
       Choose your favorite programming languages:

<asp:CheckBoxList ID="chklst" runat="server" />

<asp:Button ID="cmdOK" Text="OK" runat="server" OnClick="cmdOK_Click" />

<asp:Label ID="lblResult" runat="server" />
 </form>

</body> </html>

File: Default.aspx.cs using System; using System.Collections; using System.ruponentModel; 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; public partial class CheckBoxTest : System.Web.UI.Page {

 protected void Page_Load(object sender, System.EventArgs e)
 {
   if (this.IsPostBack == false)
   {
     chklst.Items.Add("C");
     chklst.Items.Add("C++");
     chklst.Items.Add("C#");
     chklst.Items.Add("Visual Basic 6.0");
     chklst.Items.Add("VB.NET");
     chklst.Items.Add("Pascal");
   }
 }
 protected void cmdOK_Click(object sender, System.EventArgs e)
 {
   lblResult.Text = "You chose:";
   foreach (ListItem lstItem in chklst.Items)
   {
     if (lstItem.Selected == true)
     {
       // Add text to label.
       lblResult.Text += "
" + lstItem.Text; } } lblResult.Text += "
"; }

}</source>