[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Trying to prepopulate checkbox list control

Posted on 2009-04-28
1
Medium Priority
?
530 Views
Last Modified: 2013-12-14
I have the following checkboxlist control:
<asp:CheckBoxList ID="chbxlstType"  CssClass="lbltextblcksmbld" RepeatDirection="horizontal"  runat="server">
                <asp:ListItem  Text="AE" Value="0"></asp:ListItem>
                <asp:ListItem  Text="C" Value="0"></asp:ListItem>
                <asp:ListItem  Text="NPQ" Value="0"></asp:ListItem>
                </asp:CheckBoxList>
The user will go in and check various check boxes and submit their selections to the databases. In the insert to the db we are building a string of values and inserting them into 1 varchar field. Like so:
        foreach (ListItem liType in chbxlstType.Items)
        {
            if (liType.Selected)
            {
                strTypelist += liType.Value + ",";
            }
        }
So for example, if all 3 listitems were checked and then submitted the value in the db ends up being (0,0,0). Or if for example only the first and third listitems were selected the value in the db would be (0,,0).
So now when the user comes back to the form and i need the listitems to be prepopulated with the selections the user made previously. So i need to do sometype of a comparison against the db value and check the appropriate boxes accordingly. Any suggestions as to the best way to do it in C#?
0
Comment
Question by:jazzcatone
1 Comment
 
LVL 3

Accepted Solution

by:
ChronJob earned 2000 total points
ID: 24251570
You probably want something like this.
private string getValue()
{
        foreach (ListItem liType in chbxlstType.Items)
        {
            if (liType.Selected)
            {
                strTypelist += liType.Value;
            }
            strTypelist += ",";
        }
        //trim off last comma
        strTypelist = strTypelist.substring(0,strTypelist.Length -1); 
        return strTypelist;
}
 
private void populateCheckboxes(string commaSeporatedValues)
{
       String[] valueArray = commaSeporatedValues.Split(',');
       int i = 0;
        foreach (ListItem liType in chbxlstType.Items)
        {
               liType.selected = valueArray[i].equals("0");
               i++;
        }
}

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question