取值问题

md198687 2009-12-07 04:27:14
for (int i = 0; i < this.cblNo.Items.Count; i++)
{
string noNames = "";
if (this.cblNo.Items[i].Selected)
{
string noid = this.cblNo.Items[i].Value; //得到多选框选中的id
Response.Write(noid);//输出结果是123456
}

}
怎样把123456变成1,2,3,4...
...全文
174 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuoming120 2009-12-07
  • 打赏
  • 举报
回复
//foreach (DataRow dr in sheetNames.Rows)
//{
// al.Add(dr[2]+",");
//}
jiangshun 2009-12-07
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 md198687 的回复:]
for (int i = 0; i < this.cblNo.Items.Count; i++)
{
string noNames = "";
if (this.cblNo.Items[i].Selected)
{
string noid = this.cblNo.Items[i].Value; //得到多选框选中的id
Response.Write(noid);//输出结果是123456
}

}
怎样把123456变成1 2 3 4...
分别存到数据库里面,数据库字段是int型的
[/Quote]

for (int i = 0; i < this.cblNo.Items.Count; i++)
{
string noNames = "";
string noid="";
foreach(ListItem li in this.cblNo.Items)
{
if(li.Selected)
{
//可以在这调用插入数据的方法
}
}
Response.Write(noid1)

}
mbh0210 2009-12-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jiangshun 的回复:]
建议别这样处理,假如多选框的个数大于10呢,选出来的结果可能是1,2,3,12,4
C# codefor (int i=0; i<this.cblNo.Items.Count; i++)
{string noNames="";string noid="";foreach(ListItem liinthis.cblNo.Items)
{if(li.Selected)
{
noid+=this.cblNo.Items[i].Value+",";
}
}
Response.Write(noid1)

}
[/Quote]

如果有重复,你可以在noid+=this.cblNo.Items[i].Value+",";
先判断this.cblNo.Items[i].Value+"," 是否存在,存在就不增加就ok
修改为:
if (noid.IndexOf(this.cblNo.Items[i].Value+",") == -1)
{
noid+=this.cblNo.Items[i].Value+",";

}
md198687 2009-12-07
  • 打赏
  • 举报
回复
我是要把复选框的值取出来,用int型,分别存到数据库里面
md198687 2009-12-07
  • 打赏
  • 举报
回复
for (int i = 0; i < this.cblNo.Items.Count; i++)
{
string noNames = "";
if (this.cblNo.Items[i].Selected)
{
string noid = this.cblNo.Items[i].Value; //得到多选框选中的id
Response.Write(noid);//输出结果是123456
}

}
怎样把123456变成1 2 3 4...
分别存到数据库里面,数据库字段是int型的
wartim 2009-12-07
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsApplication204
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

string noid = "123456";

MessageBox.Show(GetIt(noid));

}

String GetIt(String S)
{
if (S.Length == 0)
return String.Empty;
else if (S.Length == 1)
return S[0].ToString();
else
return S[0] + "," + GetIt(S.Remove(0, 1));
}
}
}
tkscascor 2009-12-07
  • 打赏
  • 举报
回复
lz 我怀疑你的想法 是把cblNo.Items 所有的选中项都列出来吧. 用逗号分隔?

StringBuilder str=new StringBuilder();
for (int i = 0; i < this.cblNo.Items.Count; i++)
{
string noNames = "";
if (this.cblNo.Items[i].Selected)
{
string noid = this.cblNo.Items[i].Value; //得到多选框选中的id
str.Append(noid+",");
}

}

Response.Write(str.ToString().substring(0,str.ToString().Length-1)) ;

liherun 2009-12-07
  • 打赏
  • 举报
回复

string noid="";
foreach(ListItem li in this.cblNo.Items)
{
noid += this.cblNo.Items[i].Value+",";

}
Response.Write(noid);//输出结果是123456
noid=noid.Trim(',');
md198687 2009-12-07
  • 打赏
  • 举报
回复
六楼的输出结果是1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10,
liherun 2009-12-07
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jiangshun 的回复:]
建议别这样处理,假如多选框的个数大于10呢,选出来的结果可能是1,2,3,12,4
C# codefor (int i=0; i<this.cblNo.Items.Count; i++)
{string noNames="";string noid="";foreach(ListItem liinthis.cblNo.Items)
{if(li.Selected)
{
noid+=this.cblNo.Items[i].Value+",";
}
}
Response.Write(noid1)

}
[/Quote] up
zcw840421 2009-12-07
  • 打赏
  • 举报
回复
楼上已经回答的够清楚了
jiangshun 2009-12-07
  • 打赏
  • 举报
回复
建议别这样处理,假如多选框的个数大于10呢,选出来的结果可能是1,2,3,12,4
for (int i = 0; i < this.cblNo.Items.Count; i++)
{
string noNames = "";
string noid="";
foreach(ListItem li in this.cblNo.Items)
{
if(li.Selected)
{
noid += this.cblNo.Items[i].Value+",";
}
}
Response.Write(noid1)

}
ddsxd19 2009-12-07
  • 打赏
  • 举报
回复
拼下字符串啦~

string noid = "";
for (int i = 0; i < this.cblNo.Items.Count; i++)
{
string noNames = ""; //这个没用到吗~
if (this.cblNo.Items[i].Selected)
{
if(noid != string.Empty)
{
noid += ",";
}
noid += this.cblNo.Items[i].Value; //得到多选框选中的id
}
}
Response.Write(noid);//输出你也得等到循环完再输出吧
deyter 2009-12-07
  • 打赏
  • 举报
回复

for (int i = 0; i < this.cblNo.Items.Count; i++)
{
string noNames = "";
if (this.cblNo.Items[i].Selected)
{
string noid = this.cblNo.Items[i].Value; //得到多选框选中的id
string noid1 = "";
for (int i = 0; i < noid .Length; i++)
{
noid1 += noid.substring(i,1) + ",";
}
noid1 = noid1.Trim(',');
Response.Write(noid1);//输出结果是1,2,3,4,5,6
}

}
teerhu 2009-12-07
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 liherun 的回复:]
C# codefor (int i=0; i<this.cblNo.Items.Count; i++)
{string noNames="";if (this.cblNo.Items[i].Selected)
{string noid=this.cblNo.Items[i].Value;//得到多选框选中的idchar[] c= noid.ToCharArray();
?-
[/Quote]
up
liherun 2009-12-07
  • 打赏
  • 举报
回复
for (int i = 0; i < this.cblNo.Items.Count; i++) 
{
string noNames = "";
if (this.cblNo.Items[i].Selected)
{
string noid = this.cblNo.Items[i].Value; //得到多选框选中的id
char[] c = noid.ToCharArray();
noid = "";
for (int i = 0; i < c.Length; i++)
{
noid += c[i] + ",";
}
noid = noid.Trim(',');
Response.Write(noid);//输出结果是123456
}

}
tkscascor 2009-12-07
  • 打赏
  • 举报
回复
char[] arrylist=noid.ToCharArray()

111,123

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧