Thursday, January 9, 2014

CheckBoxList Items

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" 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" AutoPostBack="true" OnSelectedIndexChanged="chklst_OnSelectedIndexChanged" /><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>

 



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;
public partial class DropDownlist_Default : 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 chklst_OnSelectedIndexChanged(object sender, 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>";
    }
    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>";
    }
}

0 comments:

Post a Comment