前台:
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="id" Width="661px">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:TemplateField HeaderText="删除" >
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="选中并删除" />
后台: protected void Button1_Click(object sender, EventArgs e)
{
string sqlText = "(";
string str = "select * from news";
for (int i = 0; i < GridView2.Rows.Count; i++)
{
//搜索第n行3列
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("chk");
if (cbx.Checked==true)
{
sqlText = sqlText + Convert.ToInt32(GridView2.DataKeys[i].Value) + ",";
}
}
//去掉最后的逗号,并且加上右括号
sqlText = sqlText.Substring(0, sqlText.Length - 1) + ")";
sqlText = "delete news where id in" + sqlText;
try
{
//执行删除语句
SqlConnection conn = yjdbclass.opennconn();
conn.Open();
SqlCommand cmd = new SqlCommand(sqlText, conn);
int delCount = Convert.ToInt32(cmd.ExecuteNonQuery());
Response.Write("<script>alert('共删除" + delCount + "条数据');</script>");
yjdbclass.gridview(str, this.GridView2);
}
catch (Exception ex)
{
//若有错误发生,输出错误信息
Response.Write(ex.Message);
}
finally
{
yjdbclass.gridview(str,this.GridView2);
} }出错:
if (cbx.Checked==true) //未将对象引用设置到对象实例
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False"
DataKeyNames="id" Width="661px">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="title" HeaderText="title" SortExpression="title" />
<asp:TemplateField HeaderText="删除" >
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="选中并删除" />
后台: protected void Button1_Click(object sender, EventArgs e)
{
string sqlText = "(";
string str = "select * from news";
for (int i = 0; i < GridView2.Rows.Count; i++)
{
//搜索第n行3列
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("chk");
if (cbx.Checked==true)
{
sqlText = sqlText + Convert.ToInt32(GridView2.DataKeys[i].Value) + ",";
}
}
//去掉最后的逗号,并且加上右括号
sqlText = sqlText.Substring(0, sqlText.Length - 1) + ")";
sqlText = "delete news where id in" + sqlText;
try
{
//执行删除语句
SqlConnection conn = yjdbclass.opennconn();
conn.Open();
SqlCommand cmd = new SqlCommand(sqlText, conn);
int delCount = Convert.ToInt32(cmd.ExecuteNonQuery());
Response.Write("<script>alert('共删除" + delCount + "条数据');</script>");
yjdbclass.gridview(str, this.GridView2);
}
catch (Exception ex)
{
//若有错误发生,输出错误信息
Response.Write(ex.Message);
}
finally
{
yjdbclass.gridview(str,this.GridView2);
} }出错:
if (cbx.Checked==true) //未将对象引用设置到对象实例
改成
CheckBox cbx = new CheckBox();
cbx = (CheckBox)GridView1.Rows[i].FindControl("chk");
这句话并没能够找到该checkbox控件,因此cbx为null
因此cbx.Checked才会报“未将对象引用设置到对象实例”错误
faint~~~
{
CheckBox cbx = (CheckBox)GridView1.Rows[i].FindControl("chk");
if (cbx .Checked == true)
{
//这里 你继续 }
}
是你循环的问题,GridView2.Rows.Count-1就对了
{
}
我这样做没问题
if( (CheckBox)GridView1.Rows[i].FindControl("chk").checked)