GridView遍历各行的控件和控件事件
2012-07-11 11:16
183 查看
<div> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand" DataKeyNames="ID"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="ID" /> <asp:BoundField DataField="Name" /> <asp:TemplateField> <ItemTemplate> <asp:Button ID="Button2" runat="server" Text="Button" CommandArgument="ID"/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = new DataTable(); dt.Columns.Add("ID", typeof(string)); dt.Columns.Add("Name", typeof(string)); for (int i = 0; i < 5; i++) { DataRow dr = dt.NewRow(); dr["ID"] = "ID" + i.ToString(); dr["Name"] = "Name" + i.ToString(); dt.Rows.Add(dr); } GridView1.DataSource = dt; GridView1.DataBind(); } } protected void Button1_Click(object sender, EventArgs e) { foreach (GridViewRow row in GridView1.Rows) { CheckBox ckb = (CheckBox)row.Cells[0].FindControl("CheckBox1"); ckb.Checked = true; } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandArgument == "ID") { GridViewRow row = ((Button)e.CommandSource).Parent.Parent as GridViewRow; CheckBox ckb = (CheckBox)row.Cells[0].FindControl("CheckBox1"); ckb.Checked = false; } }
相关文章推荐
- 在GridView控件中的HeaderTemplate模板上添加的CheckBox控件无法引发AutoPostback事件,或无规律的引发,不正常的引发
- GridView控件RowDataBound事件中获取列字段的几种途径
- 遍历指定目录下的图片文件 显示在gridview 并添加点击事件
- asp.net gridview itemtemplate中控件事件获取行参数
- GridView控件RowCreated事件的操作——鼠标动作
- Gridview事件跟它item布局中的控件事件冲突
- 遍历GridView和DataList寻找模板列的控件
- 用JavaScript获取Gridview中某个触发事件控件的ID
- 总结了一下 GridView控件的常用事件触发时序
- 关于GridView RowUpdating事件中处理一些前台特殊控件,例如 下拉框DropDownList 等等
- 扩展GridView控件(0) - 基本架构、增加事件和要点汇总
- GridView控件中的事件
- 探讨为GRIDVIEW中的控件动态添加事件
- 扩展GridView控件(0) - 基本架构、增加事件和要点汇总
- 响应 GridView 控件中的按钮事件
- 如何使用 RowDataBound 事件在数据源中的字段值显示在 GridView 控件中之前修改该值
- GridView控件事件详解
- GridView控件RowDataBound事件中获取列字段的几种途径
- GridView控件常见问题及处理方法===1. 关于隐藏字段的处理:==2. 关于按钮列的事件:==3. 关于按钮的客户端提示:==4. 关于时间字段的格式化输出:==5. 关于绑定空记录:
- Dev的Gridview控件,实现 每一行行的双击的事件