Set or obtain the checkboxlist value.

Source: Internet
Author: User

Post address: http://www.cnblogs.com/shawker/archive/2009/03/17/1414795.html

In projects, we may often encounter a situation where multiple choice information is collected. For example, when registering, we need to collect personal interests. At that time, the first thing we think of is checkboxlist. How can we get the checkboxlist value and store it in the database ??

If we still need to edit the user's personal information, and the interests can also be changed, then we will also want to use checkboxlist to display the user's individual information, so how can we use checkboxlist to represent the value in the library ?? Edit analysis question
In this case, you must think of for and foreach to traverse. If this is correct, it is okay. We can use traversal to retrieve the value of checkboxlist or set the value of checkboxlist. I have summarized the common methods and made two methods. This makes it easier and more flexible to use.

For example, we want to collect information about employees of a certain company, which is a hobby. The employee information must be changeable.

Checkboxlist is used to collect and display hobbies.

Method:

......

1. During collection, the items selected in the checkboxlist are converted into strings and separated by commas (,).
Here, you only need to call the getchecked (checkboxlist checklist, string separator) method)
You can get the desired data. Then store the data in the database.

2. Obtain the favorite data from the library (strings separated by commas (,) first ),
Then call the setchecked (checkboxlist checklist, string selval, string separator) method)
The data in the library can be displayed in the form of checkboxlist.

Method usage:
// Obtain the selected items in the checkboxlist and separate them ","
String STR = getchecked (this. checklist1 ,",");
......
// Here, the value of the STR string is set back to checkboxlist.
Setchecked (this. checklist1, STR ,",");

 

  /// <Summary> initialize the checked items in the checkboxlist. </Summary>          /// <Param name = "Checklist"> checkboxlist </param>          /// <Param name = "selval"> the selected value string is: ", 1" </param>          /// <Param name = "separator"> delimiter used in the value string, for example, comma in ", 1" </param>          Public   Static   String Setchecked (checkboxlist checklist, String Selval, String Separator) {selval = separator + selval + separator; // For example: ", 1"-> ", 1 ,"              For ( Int I = 0; I <checklist. Items. Count; I ++) {checklist. items [I]. Selected = False ; String Val = separator + checklist. items [I]. Value + separator; If (Selval. indexof (VAL )! =-1) {checklist. items [I]. Selected = True ; Selval = selval. Replace (Val, separator ); // Delete the selected value from the original value string                      If (Selval = separator) // If the last item of selval is selected, only one separator is left after replace. {Selval + = separator; // Add a separator }}} Selval = selval. substring (1, selval. Length-2 ); // Remove the added Separator              Return Selval ;} /// <Summary>          /// Obtain the value selected in the checkboxlist.          /// </Summary>          /// <Param name = "Checklist"> checkboxlist </param>          /// <Param name = "separator"> delimiter </param>          /// <Returns> </returns>         Public   Static   String Getchecked (checkboxlist checklist, String Separator ){ String Selval = "" ; For ( Int I = 0; I <checklist. Items. Count; I ++ ){ If (Checklist. items [I]. Selected) {selval + = checklist. items [I]. Value + separator ;}} Return Selval ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.