locked
using checkboxes or checkboxlist control to create a comma separated list of values to a string and input the string into the database... possible? RRS feed

  • Question

  • User-282213877 posted

    I was hoping I could use a bunch of checkboxes in Edit and Insert Mode of a formview and concatenate them together into one comma separated list of values as a string and input that string into the database field in the datasource.

    Anyone know how to do this?

    Any help would be appreciated... thanks!

    Cav

    Tuesday, January 20, 2009 7:00 PM

Answers

  • User-282213877 posted

    thanks for the help... I FINALLY figured out that you use the e.NewValues.Items(x) to set the new value... only took a week to figure that out!

    Ahhh!

     

    anyway... thanks again!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 26, 2009 7:25 PM

All replies

  • User486683203 posted

     hey Cav,

     you could do something like this

    <asp:CheckBoxList ID="testCheckBoxList" runat="server">
                <asp:ListItem Text="test1" Selected="True"></asp:ListItem>
                <asp:ListItem Text="test2" Selected="False"></asp:ListItem>
                <asp:ListItem Text="test3" Selected="True"></asp:ListItem>
                <asp:ListItem Text="test4" Selected="True"></asp:ListItem>
                <asp:ListItem Text="test5" Selected="False"></asp:ListItem>
                <asp:ListItem Text="test6" Selected="False"></asp:ListItem>
    </asp:CheckBoxList>

     

                string checkBoxValues = string.Empty;
                foreach (ListItem li in testCheckBoxList.Items)
                {
                    if (li.Selected)
                    {
                        checkBoxValues += "1";
                    }
                    else
                    {
                        checkBoxValues += "0";
                    }

                    if (testCheckBoxList.Items.IndexOf(li) != testCheckBoxList.Items.Count - 1)
                    {
                        checkBoxValues += ",";
                    }
                }

     hope that helps

     

    Aaron

    Wednesday, January 21, 2009 12:18 AM
  • User-282213877 posted

    Thanks for the reply, Aaron...

     Where does the foreach part of the code go?  Does it go in a particular event?

    I guess I should clarify... I have a formview that, when in edit mode, the checkboxlist is displayed. 

    I need to build the string from whatever is checked and then bind that to the field called "Departments" in the datasource.

    Also, I guess I need a way of reading the comma separated list of values and checking the appropriate checkboxes upon binding the formview when in edit or insert mode, so that if there is already values in that field, the appropriate checkboxes will be checked.  Hope that makes sense... ;-)

    Thanks for the help...

     

    Wednesday, January 21, 2009 9:23 AM
  • User486683203 posted

     Hey again Cavall,

     

    I am not 100% sure when dealing with formviews but I would have this code run when you are updating/adding an item.

     

    Aaron

    Wednesday, January 21, 2009 6:31 PM
  • User-282213877 posted

    Can you be more specific, please?

    ItemUpdating event, ItemUpdated event, etc.?

    How do I write the data back to the control once I have the string built?

    Thanks

    Cav

    Wednesday, January 21, 2009 6:48 PM
  • User-1635004338 posted

    Hi Cavall,

    You can set selected data in DataBound event of FormView and get the string in ItemUpdating event. You may need to add a HiddenField control into FormView to bind comma separated list to it. It is used to get the old value for updating. In my sample I suppose in your table you have a string of department such as "dep1,dep2,dep3" for each id, so you bind this string to HiddenField first in FormView. Then handling DataBound event to split the string to each individual one and selected the relevant department. When updating, you can get them all and create a new string with comma and store into the table again.

    protected void FormView1_DataBound(object sender, EventArgs e)
        {
            if (FormView1.CurrentMode == FormViewMode.Edit)
            {
                CheckBoxList cbl = (CheckBoxList)FormView1.FindControl("CheckBoxList1");
                HiddenField hf = (HiddenField)FormView1.FindControl("HiddenField1");
                string[] dep = hf.Value.Split(new Char[] {','});
                foreach (ListItem li in cbl.Items)
                {
                    for (int i = 0; i < dep.Length; i++)
                    {
                        if (li.Value == dep[i])
                        {
                            li.Selected = true;
                        }
                    }
                }
            }
        }

        protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            if (FormView1.CurrentMode == FormViewMode.Edit)
            {
                string dep = "";
                CheckBoxList cbl = (CheckBoxList)FormView1.FindControl("CheckBoxList1");
                foreach (ListItem li in cbl.Items)
                {
                    if (li.Selected)
                    {
                        dep += li.Value + ",";
                    }
                }
                dep = dep.Substring(0, dep.Length - 1);
                //do updating using this string "dep"
            }
        }

    Thanks,

    Friday, January 23, 2009 4:33 AM
  • User-282213877 posted

    Thanks for the reply... but im still confused about how the "dep" string gets posted back into the database??

    Is dep not just a local string variable in the ItemUpdating event?    How does that value ever get back into the database?

     

    Thanks!

    Friday, January 23, 2009 11:27 AM
  • User-282213877 posted

    thanks for the help... I FINALLY figured out that you use the e.NewValues.Items(x) to set the new value... only took a week to figure that out!

    Ahhh!

     

    anyway... thanks again!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 26, 2009 7:25 PM