Click here to Skip to main content
15,916,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the list of selected items from the checkbox

C#
public string getselecteditems(CheckBoxList CheckBoxList1)
    {
        var items = new StringBuilder();
        foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
                items.Append(string.Format("{0},", item.Value));

        }
        return items.ToString().TrimEnd(',');


    }


And I am trying to populate this item selected in checkbox.

C#
private void populateselecteditems(string selecteditems, CheckBoxList CheckBoxList1)
    {
        if (!string.IsNullOrWhiteSpace(selecteditems))
        {
            string[] items = selecteditems.Split(',');
            foreach (ListItem item in CheckBoxList1.Items)
            {
                item.Selected = items.Where(c => c.Equals(item.Value))
                    .FirstOrDefault() != null;
            }
        }
       

    }


Please help me how to assign this value to a textbox ( when I try the below, it shows

C#
protected void Button1_Click(object sender, EventArgs e)
    {
            // Textbox1.Text = populated values should be here. 
        //Textbox1.Text = populateselecteditems(selecteditems ,CheckBoxList CheckBoxList1);
}
</pre>
Posted
Updated 15-Jan-14 0:11am
v2

Try this:




C#
private string populateselecteditems(string selecteditems, CheckBoxList CheckBoxList1)
    {
        if (!string.IsNullOrWhiteSpace(selecteditems))
        {
            string[] items = selecteditems.Split(',');
            foreach (ListItem item in CheckBoxList1.Items)
            {
                item.Selected = items.Where(c => c.Equals(item.Value))
                    .FirstOrDefault() != null;
            }
        }
        return selecteditems;


    }




C#
protected void Button1_Click(object sender, EventArgs e)
    {
            // Textbox1.Text = populated values should be here.
        //Textbox1.Text = populateselecteditems(selecteditems ,CheckBoxList CheckBoxList1);

       Textbox1.Text=populateselecteditems(selecteditems ,CheckBoxList CheckBoxList1);
}
 
Share this answer
 
try this..

C#
protected void TextToCheckbox_Click(object sender, EventArgs e)
        {
            populateselecteditems(TextBox1.Text, CheckBoxList1);
        }
        protected void btnCheckboxtoTextbox_Click(object sender, EventArgs e)
        {
            TextBox1.Text = getselecteditems(CheckBoxList1);
        }

        private void populateselecteditems(string selecteditems, CheckBoxList CheckBoxList1)
        {
            if (!string.IsNullOrWhiteSpace(selecteditems))
            {
                string[] items = selecteditems.Split(',');
                foreach (ListItem item in CheckBoxList1.Items)
                    item.Selected = items.FirstOrDefault(k => k == item.Text) != null;

            }


        }
        public string getselecteditems(CheckBoxList CheckBoxList1)
        {
            string output = "";
            CheckBoxList1.Items.OfType<ListItem>().Where(k => k.Selected).ToList().ForEach(k => output += k + ",");
            return output.Trim(',');


        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900