Mega Code Archive

 
Categories / ASP.Net Tutorial / ASP Net Controls
 

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]));      } }