Solved

Checkboxlist problem

Posted on 2010-09-02
14
300 Views
Last Modified: 2012-05-10
Hi,

I'm using the code below to get the values of all the checkboxlist on my page .


Thanks in advance

else if (cntrl.GetType() == typeof(CheckBoxList))

                {

                    if (!string.IsNullOrEmpty(((CheckBoxList)cntrl).Text))

                    {

                        ids = ((CheckBoxList)cntrl).ID;

                        value = ((CheckBoxList)cntrl).Items.ToString();

                    }

                }





The problem I’m having is if I had a checkbox list called chkage

using the above method how do I add the selected values to 

the code below

  if (!string.IsNullOrEmpty(ids))

                {

                    Session[id] = value;



                    if (Mapp.ContainsKey(ids))

                    {

                        //now get the db parameter name

                        string newValue = Mapp[ids];



                        if (!criteria.ContainsKey(newValue))

                            criteria.Add(newValue, value);



                    }

                }

Open in new window

0
Comment
Question by:cjJosephj
  • 6
  • 5
  • 3
14 Comments
 
LVL 32

Expert Comment

by:ewangoya
ID: 33590284
string s = "";
                for (int i = 0; i < chkage.Items.Count; i++)
                    if (chkage.Items[i].Selected)
                         s = s + chkage.Items[i].Text + ";";    //do something with your selection            
               
0
 
LVL 6

Expert Comment

by:nipunu
ID: 33594425
// Get Values from CheckBoxList
String values = "";
for (int i=0; i< cbl.Items.Count; i++)
{
        if(cbl.Items[i].Selected)
        {
                values += cbl.Items[i].Value + ",";
        }
}
                       
values = values.TrimEnd(',');
0
 

Author Comment

by:cjJosephj
ID: 33595475
I used the code below but i get 2,3, but i need 2,3
              else if (cntrl.GetType() == typeof(CheckBoxList))

                {

                    if (!string.IsNullOrEmpty(((CheckBoxList)cntrl).Text))

                    {

                        id = ((CheckBoxList)cntrl).ID;

                        

                    foreach (ListItem item in ((CheckBoxList)cntrl).Items)

                    {

                        if (item.Selected)

                        {

                            value += item.Value + ",";  

                     

                    

                        }

                    }

                   

                    

                  }

                }

Open in new window

0
 
LVL 6

Expert Comment

by:nipunu
ID: 33595623
Use the following after the foreach loop

value = value.TrimEnd(',');

Example :

          foreach (ListItem item in ((CheckBoxList)cntrl).Items)
          {
                        if (item.Selected)
                        {
                            value += item.Value + ",";  
                     
                   
                        }
           }


0
 
LVL 6

Accepted Solution

by:
nipunu earned 175 total points
ID: 33595629
Sorry take this as the example


Example :

          foreach (ListItem item in ((CheckBoxList)cntrl).Items)
          {
                        if (item.Selected)
                        {
                            value += item.Value + ",";  
                     
                           
                        }
           }

           value = value.TrimEnd(',');

           //your code
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33596828
Here is a utility function you can use to append strings with any delimiter
 public string AddString(string aBaseString, string aAppend, string aDelimiter)
        {
            if (String.IsNullOrEmpty(aBaseString))
                return aAppend;
            else
                return aBaseString + aDelimiter + aAppend;
        }
in your code
foreach (ListItem item in ((CheckBoxList)cntrl).Items)
                    {
                        if (item.Selected)
                        {
                            value = AddString(value, item.Value + ",");  
                     
                   
                        }
                    }
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33596837
my mistake
foreach (ListItem item in ((CheckBoxList)cntrl).Items)
                    {
                        if (item.Selected)
                        {
                            value = AddString(value, item.Value, ",");          
                   
                        }
                    }
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:cjJosephj
ID: 33597731
Thanks guys the only problem is my query expects an int but now it is getting an interger

so i'm getting the error message
Conversion failed when converting the varchar value '2,3' to data type int.

I have increased the points to 350
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33599007
I assume your query is using this information in a where clause like

where fieldname = value

consider parsing values using a function and instead have the query do something like

 where fieldname in (select values from int_values(value))

Let me know if you need a function to parse value
0
 

Author Comment

by:cjJosephj
ID: 33599149
Not sure what changes to apply

I have this in my query
 @AgeID     varchar(100) = '',


                                                             CASE
                        WHEN @AgeID = 0 THEN 1
                        WHEN AgeID in (@AgeID) THEN 1
                        ELSE 0
                  END = 1


Thanks
0
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 175 total points
ID: 33599523
Here is a function to split the values. I copied it from a previous post

In your query, you can use it as
When AgeID in (select Value from GET_INT_TABLE(@AgeID) )..

CREATE FUNCTION [dbo].[GET_INT_TABLE]
(
      @STRINPUT VARCHAR(MAX)      
)
RETURNS @INTTABLE TABLE
(
      VALUE INT
)
AS
BEGIN
      DECLARE @insertvalue varchar(32)
      DECLARE @pos INT
      DECLARE @nextpos INT
      DECLARE @valuelen INT

      SELECT @pos = 0, @nextpos = 1
      WHILE @nextpos > 0
      BEGIN
            SELECT @nextpos = charindex(',', @STRINPUT, @pos + 1)
            SELECT @valuelen = CASE
                              WHEN @nextpos > 0 THEN
                                    @nextpos
                                          ELSE
                                    len(@STRINPUT) + 1
                                   END - @pos - 1
         
        set @insertvalue = (substring(@STRINPUT, @pos + 1, @valuelen))                      
        if IsNumeric(@insertvalue) = 1
        begin
                  INSERT @INTTABLE (VALUE)
                  VALUES (@insertvalue)
            end
            SELECT @pos = @nextpos
      END
           
      RETURN      
END
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 33599730
Remove this line from the query

 WHEN @AgeID = 0 THEN 1

so you will only have
 CASE
       When AgeID in (select Value from GET_INT_TABLE(@AgeID)) then 1
       ELSE 0
     
0
 

Author Comment

by:cjJosephj
ID: 33600427
Thanks for your help.
I have the query beloW

Which has the value 2,3 as @AGEID  but i still get the error message

Conversion failed when converting the varchar value '2,3' to data type int.


CASE

   When AGEID in (select Value from GET_INT_TABLE(@AGEID)) 

then 1 

  ELSE 0 

END = 1

Open in new window

0
 

Author Comment

by:cjJosephj
ID: 33601385
Thanks got it to work
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now