protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GV.Rows)
{
int id = Convert.ToInt32(gr.Cells[5].Text);
product pro = new product();
pro.id = id;
CheckBox ck=(CheckBox )gr.Cells [6].FindControl ("itemchck");
if (ck.Checked)
{
product.Delete(pro);
}
}
BindGridView();
}
以上你们的都不能用啊 。,我这样取出来了,可是如果我的gridview里把iD隐藏了,怎么得到呢也就是说,int id =如何获取
{
foreach (GridViewRow gr in GV.Rows)
{
int id = Convert.ToInt32(gr.Cells[5].Text);
product pro = new product();
pro.id = id;
CheckBox ck=(CheckBox )gr.Cells [6].FindControl ("itemchck");
if (ck.Checked)
{
product.Delete(pro);
}
}
BindGridView();
}
以上你们的都不能用啊 。,我这样取出来了,可是如果我的gridview里把iD隐藏了,怎么得到呢也就是说,int id =如何获取
解决方案 »
- 为什么dropdownlist,改变选项没反应啊?(我已近设置把autopostback为true)
- 用记事本写的代码在VS中怎么调试
- access的2表关联查询语句错误
- 返回数据为什么刷新
- 关于DropDownList问题
- 分都问光了啊!再来帮我个简单的问题好不啊啊,谢谢啊!
- 变量申明的时候什么时候用new?
- 急!!! 明天交货.关于后台读取页面上<Select>HTML控件的值的问题,大家帮忙,50分送上
- SOS 语法问题,关于SQL参数查询,在线等,通过上马上给分!
- 任何让页面刷新是不重复执行事件?
- |zyciis| 400求完美方案: 一个网站和多个虚拟目录共享SESSION的问题 如何解决 半年了 急
- JS动态改变img的src为GIF动画,动画却是静止的,为什么?关于实现加载状态的问题
取的时候gv.datakeys[i]
用Gv.DataKeys[e.RowIndex].Value取
我的2个方法都可以啊。、
绑定时 给 CheckBox 添加一个属性
void GridViewCheckBox_DataBinding(object sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
GridViewRow container = (GridViewRow)checkBox.NamingContainer;
myType dataobj = (myType)container.DataItem;
checkBox.InputAttributes.Add("ID", myType.getId().ToString());
}取时用此属性
public List<string> GetSelectRowID()
{
List<string> allDeleteID = new List<string>();
foreach (GridViewRow Item in GridViewData.Rows)
{
CheckBox checkBox = (CheckBox)Item.FindControl("GridViewCheckBox"); if (checkBox != null)
{
if (checkBox.Checked)
{
allDeleteID.Add(checkBox.InputAttributes["ID"].ToString());
}
}
} return allDeleteID;
}
复制、粘贴试一下吧。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<style type="text/css">
.hide { display: none; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
InsertVisible="False" ReadOnly="True" SortExpression="ProductID" >
<ControlStyle CssClass="hide" />
<FooterStyle CssClass="hide" />
<HeaderStyle CssClass="hide" />
<ItemStyle CssClass="hide" />
</asp:BoundField>
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>protected void Button1_Click(object sender, EventArgs e)
{
System.Collections.Generic.List<int> ids = new System.Collections.Generic.List<int>();
foreach (GridViewRow gvr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gvr.FindControl("CheckBox1");
if (cb.Checked)
ids.Add(Convert.ToInt32(GridView1.DataKeys[gvr.RowIndex].Value));
} Literal msg = new Literal();
string s = string.Empty;
foreach (int id in ids)
{
s += id.ToString() + ",";
}
msg.Text = "<script>alert('" + s + "');</script>";
this.Controls.Add(msg);
}
参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index源错误:
行 140: foreach (GridViewRow gr in GV.Rows)
行 141: {
行 142: int id = Convert.ToInt32(GV.DataKeys [gr.RowIndex ].Value );
行 143: product pro = new product();
行 144: pro.id = id;
源文件: d:\Backup\test\CartView.aspx.cs 行: 142 堆栈跟踪:
[ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。
参数名: index]
InsertVisible="False" ReadOnly="True" SortExpression="ProductID" >
<ControlStyle CssClass="hide" />
<FooterStyle CssClass="hide" />
<HeaderStyle CssClass="hide" />
<ItemStyle CssClass="hide" />
</asp:BoundField>这个为什么我的出不来呢。报错
.hide { display: none; }
</style>
这一段加了吗?我是劝你真的复制粘贴,然后用Northwind试一下。然后再对比。
DataKeyNames="ProductID"等等这些设置未设置可能都会引发你说的错误。所以还是劝你先Northwind一下:-),然后再说别的。