Вы находитесь на странице: 1из 2

7/10/13

Saving a user's CheckBoxList selection and re-populating the CheckBoxList from saved data

Saving a user's CheckBoxList selection and re-populating the CheckBoxList from saved data
The user has gone to the trouble of selecting a list of checkboxes to indicate their choices. Now you need to re-present their selections so that they can edit them. How do you make sure the correct checkboxes are ticked? There are two main ways to save CheckBoxList selections in a database. One is to create a new row for each value, and the other is to save the entire selection in one field as a comma-separated string. Which approach you use will depend on whether you want to do further analysis on the selections at a later stage, for example, finding out how many people selected option 2. Saving each value to a separate row makes this process very easy, whereas trying to analyse individual values in a series of comma separated strings is going to really test your string manipulation, and array skills. Whichever way you choose, you need to first iterate over the ListItem collection of the CheckBoxList to identify which Items have been selected. If you are saving to new rows for each value, you need to ExecuteNonQuery() within the loop. If you are saving one comma separated string, you can use the StringBuilder object to build the string and insert the string after the loop has built the value to be inserted. ExecuteNonQuery()
[ C # ] C h e c k B o x L i s tc h k b x=( C h e c k B o x L i s t ) F o r m V i e w 1 . F i n d C o n t r o l ( " C h e c k B o x L i s t 1 " ) ; / / S e tu pc o n n e c t i o na n dc o m m a n do b j e c t s / / O p e nc o n n e c t i o n f o r( i n ti=0 ;i<c h k b x . I t e m s . C o u n t ;i + + ) { i f( c h k b x . I t e m s [ i ] . S e l e c t e d ) { c m d . P a r a m e t e r s . A d d W i t h V a l u e ( " @ C a t e g o r y I D " ,c h k b x . I t e m s [ i ] . V a l u e ) ; c m d . E x e c u t e N o n Q u e r y ( ) ; c m d . P a r a m e t e r s . C l e a r ( ) ; } } [ V B ] D i miA sI n t e g e r D i mc h k b xA sC h e c k B o x L i s t c h k b x=C T y p e ( F o r m V i e w 1 . F i n d C o n t r o l ( " C h e c k B o x L i s t 1 " ) ,C h e c k B o x L i s t ) F o ri=0T oc h k b x . I t e m s . C o u n t-1 I fc h k b x . I t e m s ( i ) . S e l e c t e dT h e n c m d . P a r a m e t e r s . A d d W i t h V a l u e ( " @ C a t e g o r y I D " ,c h k b x . I t e m s ( i ) . V a l u e ) ; c m d . E x e c u t e N o n Q u e r y ( ) ; c m d . P a r a m e t e r s . C l e a r ( ) ; E n dI f N e x t

StringBuilder
[ c # ] C h e c k B o x L i s tc h k b x=( C h e c k B o x L i s t ) F o r m V i e w 1 . F i n d C o n t r o l ( " C h e c k B o x L i s t 1 " ) ; S t r i n g B u i l d e rs b=n e wS t r i n g B u i l d e r / / S e tu pc o n n e c t i o na n dc o m m a n do b j e c t s / / O p e nc o n n e c t i o n f o r( i n ti=0 ;i<c h k b x . I t e m s . C o u n t ;i + + ) { i f( c h k b x . I t e m s [ i ] . S e l e c t e d ) { s b . A p p e n d ( c h k b x . I t e m s [ i ] . V a l u e+" , " ) ; } } s t r i n gI n p u t S t r i n g / / R e m o v el a s tc o m m af r o ms b I n p u t S t r i n g=s b . T o S t r i n g ( ) . S u b S t r i n g ( 0 , s b . T o S t r i n g ( ) . L e n g t h ( ) 1 ) ; [ V B ] D i miA sI n t e g e r D i mc h k b xA sC h e c k B o x L i s t c h k b x=C T y p e ( F o r m V i e w 1 . F i n d C o n t r o l ( " C h e c k B o x L i s t 1 " ) ,C h e c k B o x L i s t ) D i ms bA sS t r i n g B u i l d e r=N e wS t r i n g B u i l d e r ( ) F o ri=0T oc h k b x . I t e m s . C o u n t-1 I fc h k b x . I t e m s ( i ) . S e l e c t e dT h e n s b . A p p e n d ( c h k b x . I t e m s ( i ) . V a l u e&" , " ) E n dI f N e x t ' C r e a t et h ev a l u et ob ei n s e r t e db yr e m o v i n gt h el a s tc o m m ai ns b D i mI n p u t V a l u eA sS t r i n g I n p u t V a l u e=L e f t ( s b . T o S t r i n g ( ) ,L e n ( s b . T o S t r i n g ( ) )-1 ) ' O p e nc o n n e c t i o na n dE x e c u t e S c a l a r ( )

ExecuteReader() Now that the values have been saved, they can easily be extracted and matched up to the CheckBoxList control that appears in the editing page. If the values have been saved to separate rows in the database, unless there is a good reason to bring them back in a DataSet, they will be brought back using a DataReader. The following examples assume this is the case, and during the the Read() method, each value is examined and an attempt to match the value with an existing ListItem in the CheckBoxList is made using FindByValue(). If a match is found, that ListItem's Selected property is set to True.
[ C # ] C h e c k B o x L i s tc h k b x=( C h e c k B o x L i s t ) F o r m V i e w 1 . F i n d C o n t r o l ( " C h e c k B o x L i s t 1 " ) ; w h i l e( r d r . R e a d ( ) ) { L i s t I t e mc u r r e n t C h e c k B o x=c h k b x . I t e m s . F i n d B y V a l u e ( r d r [ " I D " ] . T o S t r i n g ( ) ) ; i f( c u r r e n t C h e c k B o x! =n u l l ) { c u r r e n t C h e c k B o x . S e l e c t e d=t r u e ; } }

www.mikesdotnetting.com/Article/53/Saving-a-user's-CheckBoxList-selection-and-re-populating-the-CheckBoxList-from-saved-data

1/2

7/10/13

Saving a user's CheckBoxList selection and re-populating the CheckBoxList from saved data

[ V B ] D i mc h k b xA sC h e c k B o x L i s t c h k b x=C T y p e ( F o r m V i e w 1 . F i n d C o n t r o l ( " C h e c k B o x L i s t 1 " ) ,C h e c k B o x L i s t ) D oW h i l er d r . R e a d ( ) D i mc u r r e n t C h e c k B o xA sL i s t I t e m c u r r e n t C h e c k B o x=c h k b x . I t e m s . F i n d B y V a l u e ( r d r ( " I D " ) . T o S t r i n g ( ) ) I fc u r r e n t C h e c k B o xI s N o tN o t h i n gT h e n c u r r e n t C h e c k B o x . S e l e c t e d=T r u e E n dI f L o o p

And that just leaves how to handle the values if they are returned as a comma separated string. Once the value containing the comma-separated string has been extracted form the DataTable/DataReader (as required), the String.Split() method is used to change the string into a one-dimensional array. From that point, matching the individual values to CheckBoxList ListItem objects is the same as before:
[ C # ] s t r i n g [ ]i t e m s=r e t u r n e d _ v a l u e _ f r o m _ d b . S p l i t ( ' , ' ) ; f o r ( i n ti=0 ;i< =i t e m s . G e t U p p e r B o u n d ( 0 ) ;i + + ) { L i s t I t e mc u r r e n t C h e c k B o x=c h k b x . I t e m s . F i n d B y V a l u e ( i t e m s [ " i " ] . T o S t r i n g ( ) ) ; i f( c u r r e n t C h e c k B o x! =n u l l ) { c u r r e n t C h e c k B o x . S e l e c t e d=t r u e ; } } [ V B ] D i mi t e m sA sS t r i n g ( )=s b . T o S t r i n g ( ) . S p l i t ( " , " ) F o ri=0T oi t e m s . G e t U p p e r B o u n d ( 0 ) D i mc u r r e n t C h e c k B o xA sL i s t I t e m c u r r e n t C h e c k B o x=C h e c k B o x L i s t 2 . I t e m s . F i n d B y V a l u e ( i t e m s ( i ) . T o S t r i n g ( ) ) I fc u r r e n t C h e c k B o xI s N o tN o t h i n gT h e n c u r r e n t C h e c k B o x . S e l e c t e d=T r u e E n dI f N e x t
Tw eet

Date Posted: 03 June 2007 22:56 Last Updated: Posted by: Mikesdotnetting Total Views to date: 48806 Mike Brind 2006 - 2013. All rights reserved.

www.mikesdotnetting.com/Article/53/Saving-a-user's-CheckBoxList-selection-and-re-populating-the-CheckBoxList-from-saved-data

2/2

Вам также может понравиться