for (int i = 0; i < this.GridView1.Rows.Count; i++) { b = ((CheckBox)GridView1.Rows[i].FindControl( "CheckBox1 ")).Checked; // 如果CheckBox被选中则删除文档 if (b) { // 获取文档ID string str = GridView1.Rows[i].Cells[0].Text;
} }
例如checkbox的ID是chk,可以这样得到所有的行号:private List<int> CheckedChk = new List<int>();private void getCheckedChk() { foreach (GridViewRow row in this.GridView1.Rows) { var chk = row.FindControl("chk") as CheckBox ; if(chk !=null && chk.Checked ) CheckedChk.Add(row.DataItemIndex); } }这里收集到所有选中的航的DataItemIndex。类似地,你也可以改为收集 GridView1.DataKeys[row.DataItemIndex]。
string val = ""; foreach (GridViewRow dr in GridView1.Rows) { // i是checkbox所在列,从0开始计数 CheckBox1是checkbox的id if ((dr.Cells[i].FindControl("CheckBox1") as CheckBox).Checked) { // j是你要得到值的列 TextBox1是得到值那列的id if ("" != (dr.Cells[j].FindControl("TextBox1") as TextBox).Text.Trim()) { val = (dr.Cells[j].FindControl("TextBox1") as TextBox).Text.Trim(); } } }
<th class="th_left" width="80">
<input type="checkbox" id="all_check"/>
</th>
[code=JScript]
function CheckSelected()
{
var chks = document.getElementsByTagName("input");
var a =new Array();
var j;
for(i=0;i<chks.length;i++)
{
if(chks[i].checked&&chks[i].type=="checkbox")
{
for(j=i;j<i+1;j++)
{
a[j]=i;
}
}
}
alert(a);
if(a!="")
{
location.href='本页?Edit='+a;
}
else
{
alert('请先选择选项后再提交!');
return false;
}
}
[code]
a就是你的选择的ID集合
通过location.href=本页?Edit='+a;传到后台
在后台处理~~
{
b = ((CheckBox)GridView1.Rows[i].FindControl( "CheckBox1 ")).Checked;
// 如果CheckBox被选中则删除文档
if (b)
{
// 获取文档ID
string str = GridView1.Rows[i].Cells[0].Text;
}
}
{
foreach (GridViewRow row in this.GridView1.Rows)
{
var chk = row.FindControl("chk") as CheckBox ;
if(chk !=null && chk.Checked )
CheckedChk.Add(row.DataItemIndex);
}
}这里收集到所有选中的航的DataItemIndex。类似地,你也可以改为收集 GridView1.DataKeys[row.DataItemIndex]。
string val = "";
foreach (GridViewRow dr in GridView1.Rows)
{
// i是checkbox所在列,从0开始计数 CheckBox1是checkbox的id
if ((dr.Cells[i].FindControl("CheckBox1") as CheckBox).Checked)
{
// j是你要得到值的列 TextBox1是得到值那列的id
if ("" != (dr.Cells[j].FindControl("TextBox1") as TextBox).Text.Trim())
{
val = (dr.Cells[j].FindControl("TextBox1") as TextBox).Text.Trim();
}
}
}
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<input type="checkbox" id="name1" name="userid" checked="checked" value='<%#Eval("字段名").ToString() %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
//直接将你想要的值绑定在选择项的value中,在后台去request即可。如:一个都不选,则为null
if (Request["userid"] != null)
{
string caiwuid = Request["userid"].ToString();
}
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
string md = GridView1.Rows[i].Cells[2].Text;
} }
我是这样写的 但是还是取不到这一行列值。
{
CheckBox chk= (CheckBox)gvr.FindControl("CheckBox1");
if (chk!=null&& chk.Checked)
{
GridViewRow row = chk.NamingContainer as GridViewRow;
string s=dr.Cells[0].Text;}