asp.net中遍历CheckBox的方法
2007-06-22 21:53 Zhuang miao 阅读(4407) 评论(4) 编辑 收藏 举报在开发中经常需要判断选中的CheckBox数目,做了一下总结
首先最常见的是直接遍历页面中存在的CheckBox,基本思路就是先遍历页面中的所有控件,然后判断此控件的类型是不是System.Web.UI.WebControls.CheckBox就可以了,代码如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
第二个就是我们经常用到GridView这个控件来显示数据,经常加一列CheckBox来控制选择,全选,如何在GridView遍历CheckBox呢?我这里做了一个简单的实例
可以看见点击全选后GridView中的所有CheckBox全部被选中,实现起来如下。
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
首先对CheckBox1也就是全选的那个CheckBox添加CheckBox1_CheckedChanged事件 ,这里注意了,许多朋友这样做了但是并没成功,因为这里CheckBox的AtuoPostBack要设置为True ,否则不会改变状态。