Selasa, 27 Agustus 2013

How to use CheckBoxList Control in ASP.NET

The CheckBoxList Control

you can use a CheckBoxList control to display a number of check boxes at once as a column of check boxes. The CheckBoxList control exists within the System.Web.UI.WebControls namespace. This control is often useful when you want to bind the data from a datasource to checkboxes . The CheckBoxList control creates multiselection checkbox groups at runtime by binding these controls to a data source.

Use of CheckBoxList Control

  • Where you find multiple options.
  •  In Shoping Site where you find sub category under category 
How to use CheckBoxList Control in ASP.NET

Example of CheckBoxList Control in ASP.NET 

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string s = "Your selected books are<br/>";
        foreach (ListItem s1 in CheckBoxList1.Items)
        {
            if (s1.Selected)
            {
                s = s + s1.Text + "<br/>";


            }
        }
        result.Text = s;

    }

    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div >
Select Your Favorite books:<br />
            <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True">
                <asp:ListItem>ASP.NET </asp:ListItem>
                <asp:ListItem>WINDOWS FORMS</asp:ListItem>
                <asp:ListItem>WPF</asp:ListItem>
                <asp:ListItem>WCF</asp:ListItem>
                <asp:ListItem>JAVA SCRIPT</asp:ListItem>
            </asp:CheckBoxList>


        </div>
        <asp:Label ID="result" runat="server"></asp:Label>
    </form>
</body>
</html>


Output
How to use CheckBoxList Control in ASP.NET

This example shows that you can choose multiple item in given list. Here foreach loop is running from starting index to ending imdex. When you select ASP.NET book item in the given list then your item will appear on the Label control under the CheckBoxList. If you again select Windows Forms Book in given list , PostBack will occurs and your result will appear on Label Control with previous result.   

Tidak ada komentar:

Posting Komentar