求助!关于CHECKBOX

hejinghj 2003-10-15 04:44:50
一段程序如下:
private void Button2_Click(object sender, System.EventArgs e)
{
int rowCnt;
int rowCtr;
int cellCtr;
int cellCnt;

rowCnt = int.Parse(TextBox1.Text);
cellCnt = int.Parse(TextBox2.Text);

for(rowCtr=1; rowCtr <= rowCnt; rowCtr++)
{

TableRow tRow = new TableRow();
Table2.Rows.Add(tRow);
for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{

TableCell tCell = new TableCell();
tRow.Cells.Add(tCell);
string prodID = rowCtr + "_" + cellCtr;
CheckBox Box = new CheckBox();
tCell.Controls.Add(Box);
Box.Text = rowCtr + ":" + cellCtr;

}
}
}
我该如何获得用户都选择了哪些CheckBox呢?
...全文
51 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
hejinghj 2003-10-20
  • 打赏
  • 举报
回复
怎么赋ID值?
Abac 2003-10-20
  • 打赏
  • 举报
回复
你给生成的checkbox赋个ID值,没有ID当然不能操作其ID了。
hejinghj 2003-10-18
  • 打赏
  • 举报
回复
TO 阿拉丁:
if ( tabCell.Controls[j].ID.IndexOf("CheckBox_selected")>-1 )

这句过不去.
提示:
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
skyover 2003-10-18
  • 打赏
  • 举报
回复
我这是从Asp转移过来的老土办法,不行不要怪我哦。哈哈。不过我这里是可以用了。
skyover 2003-10-18
  • 打赏
  • 举报
回复
假设你给Checkbox设置一个ID叫做CKBOX,那么在页面里生成的代码就会是CKBOX1,CKBOX2.....

你可以这样取得:

Dim C As System.Collections.Specialized.NameValueCollection = Request.Form
Dim _CK As String = Nothing
Dim i As Integer
For i = 0 To C.Count - 1
If C.GetKey(i).IndexOf("CKBOX") <> -1 Then
_CK = _CK & "|" & C.Item(i)
End If
Next

这样就就取得了用户所选的所有Checkbox的值了,他们以|连接在一起。
hejinghj 2003-10-17
  • 打赏
  • 举报
回复
有没有人可以帮我一下呀.
现在用FOREACH 根本找不到CHECKBOX. 我该怎么判断用户已经选择了哪些CHECKBOX呀?
阿拉丁 2003-10-17
  • 打赏
  • 举报
回复
// Pickup input
for ( int i = 0;i < Table3.Rows.Count ; i++ )
{
TableRow tabRow = Table3.Rows[i];
for ( int j = 0;j < tabRow.Cells.Count ; j++ )
{
//int j = 1;
TableCell tabCell= tabRow.Cells[j];
if ( tabCell.Controls[k].ID.IndexOf("CheckBox_selected")>-1 )
{
CheckBox cbx = ( CheckBox )tabCell.Controls[k];
int iRow = Convert.ToInt32(cbx.ID.Replace("CheckBox_selected",""));
PickTable.Rows[iRow - 1]["selected"] = cbx.Checked.ToString();
PickHash.Add( cbx.ID,cbx.Checked.ToString());
break;
}
}
}
zsww 2003-10-17
  • 打赏
  • 举报
回复
给你参考一下:

<%@ Page %>
<html>
<head>
<script language="C#" runat="server">
void Button1_Click(object Source, EventArgs e) {
String s = "选择您选择了:<br>";
//通过遍历来判断哪些选项被选中
for (int i=0; i < Check1.Items.Count; i++)
{
if ( Check1.Items[ i ].Selected )
{
// 将选中的选项添加到S变量中
s = s + Check1.Items[i].Text;
s = s + "<br>";
}
}
Label1.Text = s;
}

void chkLayout_CheckedChanged(Object sender, EventArgs e) {
//设置RepeatLayout属性
if (chkLayout.Checked == true) {
Check1.RepeatLayout = RepeatLayout.Table;
}
else {
Check1.RepeatLayout = RepeatLayout.Flow;
}
}

void chkDirection_CheckedChanged(Object sender, EventArgs e) {
//设置RepeatDirection属性
if (chkDirection.Checked == true) {
Check1.RepeatDirection = RepeatDirection.Horizontal;
}
else {
Check1.RepeatDirection = RepeatDirection.Vertical;
}
}
</script>

</head>
<body>
<form runat=server>
您喜欢的城市:
<asp:CheckBoxList id=Check1 runat="server">
<asp:ListItem>大连</asp:ListItem>
<asp:ListItem>苏州</asp:ListItem>
<asp:ListItem>昆明</asp:ListItem>
</asp:CheckBoxList>
<p>

<asp:CheckBox id=chkLayout OnCheckedChanged="chkLayout_CheckedChanged"
Text="以表格的形式显示" Checked=true AutoPostBack="true" runat="server" />

<br>
<asp:CheckBox id=chkDirection OnCheckedChanged="chkDirection_CheckedChanged"
Text="纵向排列" AutoPostBack="true" runat="server" />

<p>
<asp:Button id=Button1 Text="提交" onclick="Button1_Click" runat="server"/>
<p>

<asp:Label id=Label1 font-size="10pt" runat="server"/>

</form>
</body>
</html>



-----------努力学习 不断实践 虚心讨教---------
hejinghj 2003-10-17
  • 打赏
  • 举报
回复
用CheckBoxList控件试试!!!
**********************************
可是用CheckBoxList绑定数据库后,可以实现,一行显示三个.多行显示吗?
我做了一个只能一行显示一个.显示多行.
zsww 2003-10-17
  • 打赏
  • 举报
回复
用CheckBoxList控件试试!!!

-----------努力学习 不断实践 虚心讨教---------
hejinghj 2003-10-16
  • 打赏
  • 举报
回复
还是不行呀.
if(ctl.GetType().ToString()=="System.Web.UI.WebControls.CheckBox")
我跟踪了一下,执行到这句后条件永远成立不了,好象没有找到CheckBox,我的TABLE2上有6个
CheckBox呢,可是他只循环了两遍呀.
cyp503 2003-10-16
  • 打赏
  • 举报
回复


sorry

应该改成


foreach(Control ctl in Table2.Controls)
{
if(ctl.GetType().ToString()=="System.Web.UI.WebControls.CheckBox")
{
CheckBox cb = (CheckBox)ctl;
if(cb.Checked)
Response.Write(ctl.ID.ToString() " Checked!");

}

}
hejinghj 2003-10-16
  • 打赏
  • 举报
回复
private void Button2_Click(object sender, System.EventArgs e)
{
int rowCnt;
int rowCtr;
int cellCtr;
int cellCnt;

rowCnt = int.Parse(TextBox1.Text);
cellCnt = int.Parse(TextBox2.Text);

for(rowCtr=1; rowCtr <= rowCnt; rowCtr++)
{

TableRow tRow = new TableRow();
Table2.Rows.Add(tRow);
for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{

TableCell tCell = new TableCell();
tRow.Cells.Add(tCell);
string prodID = rowCtr + "_" + cellCtr;
CheckBox Box = new CheckBox();
tCell.Controls.Add(Box);
Box.Text = rowCtr + ":" + cellCtr;

}
}
}


private void Button1_Click(object sender, System.EventArgs e)
{
foreach(Control ctl in this.Table2.Controls )
{
TextBox1.Text= Label1.Text;
Label1.Text=ctl.GetType().ToString();

if(ctl.GetType().ToString()=="System.Web.UI.WebControls.CheckBox")
{
CheckBox cb = (CheckBox)ctl;
if(cb.Checked)
{
Label1.Text=Label1.Text+cb.ID.ToString();
TextBox1.Text=TextBox1.Text+cb.ID.ToString();
}
}


}

}
cyp503 2003-10-16
  • 打赏
  • 举报
回复

请把代码贴一下
hejinghj 2003-10-16
  • 打赏
  • 举报
回复
通过checkbox Name循环判断那的checked的值为true.

这个具体的应该怎么写?请批教.
************************************
在Button2_Click中添加的控件,刷新后会消失的
我现在已经改成是在页面加载时添加控件,刷新后还会消失吗?我该如何做?

非常感谢各位的帮助.

icyer 2003-10-16
  • 打赏
  • 举报
回复
在Button2_Click中添加的控件,刷新后会消失的
houlinghouling 2003-10-16
  • 打赏
  • 举报
回复
通过checkbox Name循环判断那的checked的值为true.
hejinghj 2003-10-16
  • 打赏
  • 举报
回复
循环ID? 什么意思? 请详细解释一下好吗?
yuhang001 2003-10-16
  • 打赏
  • 举报
回复
循环ID
hejinghj 2003-10-15
  • 打赏
  • 举报
回复
cyp503(谁怕?一蓑烟雨任平生) 用你的方法出现以下错误信息:
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidCastException: 指定的转换无效。

这是怎么回事?
加载更多回复(2)

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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