怎么判断asp.net里的checkbox是否选中?急切中

speedyen 2003-09-15 02:13:27
我的代码如下:
Dim i As Integer
For i = 0 To DataGrid1.Items.Count - 1
Dim cb As CheckBox
cb = CType(Me.DataGrid1.Items(0).FindControl("CheckBox2"), CheckBox)

If cb.Checked = True Then
Response.Write("yes")
Else
Response.Write("no")
End If

显示的总是"no",请指点,谢谢!!
...全文
152 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
speedyen 2003-09-15
  • 打赏
  • 举报
回复
if(!Page.isPostBack)
{
//取得数据并帮定DataGrid
}

button1_click(object e,EventArgs e)
{
//放上你的代码
}

确实把上面的代码加进去就OK了,多谢 memoriccell(记忆细胞)
memoriccell 2003-09-15
  • 打赏
  • 举报
回复
我前两天也碰到了。
你是不是点击一个按钮,然后搜索在DataGrid中选中的CheckBox?
这样的话必须把绑定DataGrid的代码放在if(!Page.isPostBack) { }里面,
否则每次点击按钮都会重新绑定DataGrid(现有状态全部消失),当然总是no了。

你的代码是对的!
只要这样:
if(!Page.isPostBack)
{
//取得数据并帮定DataGrid
}

button1_click(object e,EventArgs e)
{
//放上你的代码
}
树猫 2003-09-15
  • 打赏
  • 举报
回复
都不对呢!!!????
树猫 2003-09-15
  • 打赏
  • 举报
回复
string str = null;
foreach(DataGridItem item in dgrdAptm.Items)
{
CheckBox chk = (CheckBox)item.Cells[0].FindControl("chkID");
if(chk.Checked)
{
str += "yes";
}
}
// Server.Transfer("DgrdChk.aspx?id="+str);

试了数次,都不行啊,根本检测不出选中项
树猫 2003-09-15
  • 打赏
  • 举报
回复
可是如何得到选中的 checkBox 的 value 值
wirte 2003-09-15
  • 打赏
  • 举报
回复
foreach(DataGridItem m_item in this.DataGrid2.Items)
{
if(((CheckBox)m_item.Cells[0].Controls[1])).Checked==true)
{
Response.Write("yes");
}
Else
Response.Write("no");
}
workforwork 2003-09-15
  • 打赏
  • 举报
回复
Dim dgItem As DataGridItem
Dim chkSelected As CheckBox

For Each dgItem In DataGrid1.Items
chkSelected = dgItem.FindControl("chk_check")
If chkSelected.Checked Then
Dim i_index As Integer = Integer.Parse(dgItem.ItemIndex)
Dim tmp As String
tmp = DataGrid1.DataKeys(i_index)
Response.Write(tmp) 'tmp仅为测试,打印当前的ItemIndex值
End If
Next

要设datagrid中的datakeyfield属性。
seesea125 2003-09-15
  • 打赏
  • 举报
回复
我发的c#的,你用vb的话
foreach(DataGridItem m_item in this.DataGrid2.Items)
Dim cb As CheckBox
cb=CType(m_item.Cells(1).FindControl("CheckBox2"),checkbox)
if cb.Checked then
Response.Write("yes")
Else
Response.Write("no")
End if
next
seesea125 2003-09-15
  • 打赏
  • 举报
回复
我发的c#的,你用vb的话
foreach(DataGridItem m_item in this.DataGrid2.Items)
Dim cb As CheckBox
cb=CType(m_item.Cells(1).FindControl("CheckBox2"),checkbox)
if cb.Checked then
Response.Write("yes")
Else
Response.Write("no")
End if
next
阿拉丁 2003-09-15
  • 打赏
  • 举报
回复
呵呵,.js .cs都能实现的
speedyen 2003-09-15
  • 打赏
  • 举报
回复
Dim i As Integer
改成这样还不行,我才学asp.net,多谢指点!!!
For i = 0 To DataGrid1.Items.Count - 1
Dim cb As CheckBox
cb = CType(Me.DataGrid1.Items(i).Cells(1).FindControl("CheckBox2"), CheckBox)

If cb.Checked Then
Response.Write("yes")
Else
Response.Write("no")
End If
Next
acewang 2003-09-15
  • 打赏
  • 举报
回复
别开玩笑了,怎么会实现不了。
搜一搜以前的帖子,有很多。都快懒得给你找了。
树猫 2003-09-15
  • 打赏
  • 举报
回复
这个好像要用 js 才行,我用c#没有作出来

var chks = document.getElementsbyid(id);
/*-----------------------------------------------------------*/
/* 函数功能:返回选中的 checkbox 的值的数组 */
/* 参数chks:checkbox 控件数组 */
/* 返回值 :选中的 checkbox 的值的数组 */
/* 作者 :张小斌 */
function GetChkVlas(chks)
{
var arrChks = new Array();
arrChks = chks;

var iNum = arrChks.length;
var arrVal = new Array();

var j = 0;
for(i=0;i<iNum;i++)
{
if(arrChks[i].checked)
{
arrVal[j] = arrChks[i].value;
j++;
}
}

return arrVal;
}
/**---------------------------------------------------------*/
xyw7899 2003-09-15
  • 打赏
  • 举报
回复
If cb.Checked = True 是否错了???
seesea125 2003-09-15
  • 打赏
  • 举报
回复
foreach(DataGridItem m_item in this.DataGrid2.Items)
{
if(((CheckBox)m_item.Cells[0].FindControl("cb")).Checked==true)
{
Response.Write("yes");
}
Else
Response.Write("no");
}

62,041

社区成员

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

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

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

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