当我们检查多个chkbox时,重复相同的值 [英] The same value is repeat when we check more than one chkbox

查看:89
本文介绍了当我们检查多个chkbox时,重复相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我检查多个复选框时,它重复相同的值,如下面的

当我clik 1表示它似乎1

当我点击1和2意味着它似乎1,1,2





我尝试过:



这是我的代码

  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]);
}

解决方案

试试这个



 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]);
}


 ViewState [  vallue] = val.Remove(val.Length  -   1  .ToString(); 



val 已经是一个字符串了!



当你不理解你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看你的代码是什么这样做。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个错误。



引用:

我知道什么是调试Sir ..我做了调试我只看到那里的值

显然你没有调试你的代码而你没有知道如何使用调试器。


在将值添加到所选复选框的ViewState之前,请检查该视图状态中是否存在此选定值。如果在视图状态中存在选中的复选框值,则不要在该视图状态中添加此值。



再次检查是否取消选中所选复选框,然后从视图状态中删除复选框值

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

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"]);
  }

解决方案

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"]);
        }


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.


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.


这篇关于当我们检查多个chkbox时,重复相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆