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







1 comment: