I NEED TO DISPLAY MY CHECKBOXLIST SELECTED ITEMS IN A LABEL..HOW TO DO???

mohan kumar

Total Post:10

Points:70
Posted by  mohan kumar
C# 
C#
 7726  View(s)
Ratings:
Rate this:
This is my aspx page:-

<div>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true" 
            onselectedindexchanged="CheckBoxList1_SelectedIndexChanged"
          >
        </asp:CheckBoxList>
        <br />
        <br />
        <asp:Label ID="lblChkSelItems" runat="server"></asp:Label>
    </div>

This is my code behind page:-

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int i=0;
        string msg = "You have selected the following items: ";
        if (CheckBoxList1.Items[i].Selected)
        {
            msg = msg + CheckBoxList1.Items[i].Text + "<br>";
          
        }
        lblChkSelItems.Text = msg;
    }

Even after enabling autopostback option as true,i cant get my required result.. Help me out guyz!!!!
  1. Chris Anderson

    Post:65

    Points:455
    Re: I need to display my CheckboxList selected items in a label..How to do???

    Hi,

    You can use the code give below:

    <div>
            <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true">
            </asp:CheckBoxList>
            <br />
            <br />
            <asp:Label ID="lblChkSelItems" runat="server"></asp:Label>
    </div>

    protected void Page_Load(object sender, EventArgs e)
    {
                if (!Page.IsPostBack)
                {
                    string[] country = { "India", "USA", "Canada" };
                    for (int i = 0; i < country.Length; i++)
                        CheckBoxList1.Items.Add(country[i]);
                }
                else
                {
                    string msg = "You have selected the following items: ";
                    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    {
                        if (CheckBoxList1.Items[i].Selected)
                            msg = msg + CheckBoxList1.Items[i].Text + "<br>";
                    }
                    lblChkSelItems.Text =  msg;
                }
            }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!