GridView中CheckBox、CheckBoxField取值的方法集
2011-12-27 22:29
309 查看
直接通过GridView的cells取CheckBox的值都是空串,在网上查询之后,有三种解决方案,将其收藏。
一、CheckBoxField取值的方法:
前台代码:
<asp:GridView ID="GridView1" runat="server" Width="418px">
<Columns>
<asp:CheckBoxField DataField="是否有效" HeaderText="有效性" />
</Columns>
</asp:GridView>
后台代码:
CheckBox cb = (CheckBox)GridView_Users.SelectedRow.Cells[6].Controls[0];
if(cb!=null) CheckBox_Available.Checked = cb.Checked;
二、CheckBox取值:
方法一:(C#)
操作:
view plain
string PKname="";
foreach (GridViewRow GR in this.GridView1.Rows)
{
CheckBox CB = (CheckBox)GR.FindControl("CheckBox1");
if (CB.Checked)
{
PKname += this.GridView1.DataKeys[GR.RowIndex].Value.ToString()+",";
}
}
补充:前台<asp:CheckBox ID="CheckBox1" runat="server" Text=' <%#Eval("列名") %>'/>
实例:前台代码
view plain
<Columns>
<asp:TemplateField HeaderText="选择">
<HeaderStyle HorizontalAlign="Center" Height="25px" Width="45px" />
<ItemTemplate>
<asp:CheckBox ID="ckb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sid" HeaderText="编号" />
<asp:BoundField DataField="cname" HeaderText="姓名" />
</Columns>
实例:后台代码
view plain
foreach (GridViewRow gvr in this.GridView1.Rows)
{
Control ctl = gvr.FindControl("ckb");
CheckBox ck = (CheckBox)ctl;
if (ck.Checked)
{
TableCellCollection cell = gvr.Cells;
string wid += cell[1].Text+",";
}
}
方法二:(VB)
GridView->设置DataKeyNames
view plain
Dim gvr as GridViewRow
Dim KeyName as string'要的关键字,实际就是数据表的主键.需要事先在GridView1的DataKeyNames中设置.
Dim i as IntegerFor each gvr in GridView1.Rows
IF Ctype(gvr.FindControl("CheckBox1"),CheckBox).Checked=True Then
i=gvr.Rowindex;'GridView行索引
KeyName=GridView1.DataKeys(i).value;
...根据KeyName想做什么做什么吧.
End If
Next
一、CheckBoxField取值的方法:
前台代码:
<asp:GridView ID="GridView1" runat="server" Width="418px">
<Columns>
<asp:CheckBoxField DataField="是否有效" HeaderText="有效性" />
</Columns>
</asp:GridView>
后台代码:
CheckBox cb = (CheckBox)GridView_Users.SelectedRow.Cells[6].Controls[0];
if(cb!=null) CheckBox_Available.Checked = cb.Checked;
二、CheckBox取值:
方法一:(C#)
操作:
view plain
string PKname="";
foreach (GridViewRow GR in this.GridView1.Rows)
{
CheckBox CB = (CheckBox)GR.FindControl("CheckBox1");
if (CB.Checked)
{
PKname += this.GridView1.DataKeys[GR.RowIndex].Value.ToString()+",";
}
}
补充:前台<asp:CheckBox ID="CheckBox1" runat="server" Text=' <%#Eval("列名") %>'/>
实例:前台代码
view plain
<Columns>
<asp:TemplateField HeaderText="选择">
<HeaderStyle HorizontalAlign="Center" Height="25px" Width="45px" />
<ItemTemplate>
<asp:CheckBox ID="ckb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="sid" HeaderText="编号" />
<asp:BoundField DataField="cname" HeaderText="姓名" />
</Columns>
实例:后台代码
view plain
foreach (GridViewRow gvr in this.GridView1.Rows)
{
Control ctl = gvr.FindControl("ckb");
CheckBox ck = (CheckBox)ctl;
if (ck.Checked)
{
TableCellCollection cell = gvr.Cells;
string wid += cell[1].Text+",";
}
}
方法二:(VB)
GridView->设置DataKeyNames
view plain
Dim gvr as GridViewRow
Dim KeyName as string'要的关键字,实际就是数据表的主键.需要事先在GridView1的DataKeyNames中设置.
Dim i as IntegerFor each gvr in GridView1.Rows
IF Ctype(gvr.FindControl("CheckBox1"),CheckBox).Checked=True Then
i=gvr.Rowindex;'GridView行索引
KeyName=GridView1.DataKeys(i).value;
...根据KeyName想做什么做什么吧.
End If
Next
相关文章推荐
- GridView中CheckBox、CheckBoxField取值的方法集
- GridView中CheckBox、CheckBoxField取值的方法集
- GridView中CheckBox、CheckBoxField取值的方法
- html中checkbox在GridView选中并取值方法(转载)
- GridView中CheckBox、CheckBoxField取值的方法集(转)
- CheckBoxField取值的方法
- Gridview的item含有checkbox,setOnItemClickListener方法失效的问题
- 从GridView 中取值的方法汇总
- ASP.NET2.0中的GRIDVIEW控件在使用TemplateField中的LinkButton时如何在RowCommand事件中找到当前行index的方法
- jQuery操作CheckBox的方法(选中,取消,取值)
- jQuery操作CheckBox的方法介绍(选中,取消,取值)
- Gridview, ListView中的item含有checkbox,setOnItemClickListener方法失效的问题
- GridView中出现“TemplateField 上不支持回调,因为某些控件不能在回调中正确更新。”的解决方法
- checkbox嵌套在repeater,gridview中的全选/反选(方法总结)
- ASP.NET中GridView的CheckBoxField属性DataField的值
- GridView 用 checkbox 全选并取值
- GridView 中取值的方法汇总
- jQuery操作CheckBox的方法(选中,取消,取值)详解
- GridView中BoundField类型列的改行方法
- jQuery操作CheckBox的方法介绍(选中,取消,取值)