Home > DeveloperSection > Forums > I need to display my CheckboxList selected items in a label..How to do???
mohan kumar

Total Post:10

Points:70
Posted on    November-15-2011 12:49 AM

 C# C# 
Ratings:


 1 Reply(s)
 6075  View(s)
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!!!!


Chris Anderson
Chris Anderson

Total Post:65

Points:455
Posted on    November-15-2011 5:46 AM

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;
            }
        }

Don't want to miss updates? Please click the below button!

Follow MindStick