Monday, September 27, 2010

CheckBoxList Control In ASP.NET

Getting CheckBoxList Selected Item Values into Text Box:



ASPX File Code:


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
        onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
        <asp:ListItem>a</asp:ListItem>
        <asp:ListItem>b</asp:ListItem>
        <asp:ListItem>c</asp:ListItem>
        <asp:ListItem>d</asp:ListItem>
    </asp:CheckBoxList>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    </form>
</body>
</html>


.CS Code:

We want to write code in CheckBoxList1_SelectedIndexChange Event

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = string.Empty;

        foreach (ListItem listitem in CheckBoxList1.Items)
        {
            if (listitem.Selected)
                TextBox1.Text += listitem.Text + ",";
        }
    }

OUTPUT:

Get the output for above discussed code from the following link:

           
                                            CheckBoxList Control Selected Items







2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
    or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

    ReplyDelete