ASP.NET Tutorial/ASP.net Controls/CheckBoxList
Содержание
Adding ListItems Declararatively
<%@ 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">
<div>
<h1>CheckBoxList Control</h1>
<h2>Adding ListItems Declararatively</h2>
<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>
</div>
</form>
</body>
</html>
Adding ListItems from an Array
<%@ 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">
<div>
<h1>CheckBoxList Control</h1>
<h2>Adding ListItems from an Array</h2>
<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>
</div>
</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]));
}
}
}
Adding ListItems from an Array Using DataBinding
<%@ 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">
<div>
<h1>CheckBoxList Control</h1>
<h2>Adding ListItems from an Array Using DataBinding</h2>
<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>
</div>
</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();
}
}
Adding ListItems from an Array With a Value
<%@ 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">
<div>
<h1>CheckBoxList Control</h1>
<h2>Adding ListItems from an Array With a Value</h2>
<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>
</div>
</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]));
}
}
}
Fill data into asp:CheckBoxList and get selected items (C#)
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">
<div>
Choose your favorite programming languages:<br /><br />
<asp:CheckBoxList ID="chklst" runat="server" /><br /><br />
<asp:Button ID="cmdOK" Text="OK" runat="server" OnClick="cmdOK_Click" />
<br /><br />
<asp:Label ID="lblResult" runat="server" />
</div>
</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:<b>";
foreach (ListItem lstItem in chklst.Items)
{
if (lstItem.Selected == true)
{
// Add text to label.
lblResult.Text += "<br />" + lstItem.Text;
}
}
lblResult.Text += "</b>";
}
}