Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i check more than one check box it repeat the same value like below
 
when i clik 1 means it seems 1
 
when i click 1 and 2 means it seems 1,1,2 


What I have tried:

This is My code
C#
protected void cckk_SelectedIndexChanged(object sender, EventArgs e)
  {
      string val = string.Empty;
      StringBuilder sb = new StringBuilder();
      foreach (ListItem it in cckk.Items)
      {
          if (it.Selected)
          {

              val += sb.Append(it.Value + ',');
              ViewState["vallue"] = val.Remove(val.Length - 1).ToString();

           }
      }
      string a1a = Convert.ToString(ViewState["vallue"]);
  }
Posted
Updated 1-Feb-17 8:12am
Comments
ZurdoDev 1-Feb-17 7:04am    
Debug your code and find out what is happening.
Member 12857356 1-Feb-17 7:06am    
When i click one checkbox means itz working fine. for ex It seems like 1

But When i click more than one checkbox means it seems like 1,1,2
ZurdoDev 1-Feb-17 7:09am    
Debug your code. You can watch exactly what happens as each line executes. You'll find the issue very, very fast.
Member 12857356 1-Feb-17 7:13am    
for Example

1 2 3 4 5 these are all checkbox


When i click 1 it seems 1
When i click 1 and 2 and 3 means it seems like 1,1,2,1,2,3
ZurdoDev 1-Feb-17 7:18am    
You keep explaining what you see happening in the output. Do you understand what debugging means? It means put a breakpoint on your first line of code and then step through the code one line at a time and inspect what the values in your variables are. If you do that, you will find the answer quicker than how much time you have spent here asking us to do it for you.

try this

protected void cckk_SelectedIndexChanged(object sender, EventArgs e)
        { 
            List<string> lst = new List<string>();
            foreach (ListItem it in cckk.Items)            
                if (it.Selected)                
                    lst.Add(it.Value);

            ViewState["vallue"] = string.Join(",", lst);
            string a1a = Convert.ToString(ViewState["vallue"]);
        }
 
Share this answer
 
C#
ViewState["vallue"] = val.Remove(val.Length - 1).ToString();

val is already a string !

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

Quote:
I know what is debugging Sir..I done the debugging the i see the values there only

Obviously you didn't debug your code ir you don't know how to use the debugger.
 
Share this answer
 
v2
Before adding the value into the ViewState of selected checkbox, check whether this selected value is exists or not in that viewstate. If Selected Checkbox value is exists in the viewstate then dont add this value in that viewstate.

Again check if the selected checkbox is unchecked then remove the checkbox value from the viewstate.
 
Share this answer
 
If you debug your code, the problem will become obvious:
C#
val += sb.Append(it.Value + ',');

For each checked item, you:
  1. Take the value of the checked item, and append a comma;
  2. Append the value and comma to the StringBuilder;
  3. Convert the StringBuilder to a String, and append it to the val;

So, if you've checked "1", "2" and "3":
  1. sb: "1,";
    val: "1,"
  2. sb: "1,2,";
    val: "1," + "1,2," = "1,1,2,";
  3. sb: "1,2,3,";
    val: "1,1,2," + "1,2,3," = "1,1,2,1,2,3,";


Use the code that Karthik posted in Solution #2[^] to get the correct result.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900