Friday 16 December 2011

Check & UnCheck Item Using CheckBoxList and Display It's Value in Asp.Net




Check & UnCheck Item Using CheckBoxList and Display It's Value in Asp.Net




Default.aspx
-----------------------------

 <form id="form1" runat="server">
    <div>
   
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="True"
            onselectedindexchanged="CheckBoxList1_SelectedIndexChanged">
            <asp:ListItem>One</asp:ListItem>
            <asp:ListItem>Two</asp:ListItem>
            <asp:ListItem>Three</asp:ListItem>
        </asp:CheckBoxList>
   
    </div>
    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Checked All</asp:LinkButton>
    <p>
        <asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">UnCheck All</asp:LinkButton>
    </p>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </form>
------------------------------

Default.aspx.cs
---------------

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Label1.Text = "";
        foreach(ListItem li in CheckBoxList1.Items)
        {
            li.Selected = true;
            Label1.Text +="<br>"+ li.Text;
        }
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {

        foreach (ListItem li in CheckBoxList1.Items)
        {
            li.Selected = false;
            Label1.Text = "";
        }

    }
    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string str = string.Empty;
        Label1.Text = "";
        foreach (ListItem li in CheckBoxList1.Items)
        {
          
           if(li.Selected)
           {
               str+="<br>"+ li.Text;
           }
        }
        Label1.Text = str;

    }
}








demo of Check & UnCheck Item Using CheckBoxList and Display It's Value in Asp.Net


Share This
Previous Post
Next Post

2 comments:

  1. Really nice blog and good details of asp.net.
    what is m2m

    ReplyDelete
  2. really helpful for us and plz try to provide some video tuts :)

    ReplyDelete