protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string xjg=this.TextBox1.Text;
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox lb1 = (CheckBox)gr.FindControl("CheckBox1");
if (lb1.Checked=true)
{
strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[0]["gjid"].ToString() + "";
shuju.ExcuteSql(strSql);
}
}
}
GridView1控件中有CheckBox,我想批量修改我选中的其fy的值 ,但结果是我点击按钮时,出现的是它把我所有的CheckBox全部都选中,修改了第一条数据
{
string xjg=this.TextBox1.Text;
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox lb1 = (CheckBox)gr.FindControl("CheckBox1");
if (lb1.Checked=true)
{
strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[0]["gjid"].ToString() + "";
shuju.ExcuteSql(strSql);
}
}
}
GridView1控件中有CheckBox,我想批量修改我选中的其fy的值 ,但结果是我点击按钮时,出现的是它把我所有的CheckBox全部都选中,修改了第一条数据
{
string xjg=this.TextBox1.Text;
string strSql="";
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox lb1 = (CheckBox)gr.FindControl("CheckBox1");
if (lb1.Checked=true)
{
strSql += "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[0]["gjid"].ToString() + ";";
}
} if(strSql!=""){
shuju.ExcuteSql(strSql); }
}
这样的话只能找到checkbox1吧,所以只修改了这一个
strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[0]["gjid"].ToString() + "";
看这句sql只会改变一次数据库啊
dtTable.Rows[i]["gjid"].ToString()
改
if (lb1.Checked==true)
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox lb1 = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (lb1.Checked== true)
{
strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[i]["gjid"].ToString() + "";
shuju.ExcuteSql(strSql);
} }
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{ strSql = "Update sc_gj set fy='" + xjg + "' where gjid=" + dtTable.Rows[i]["gjid"].ToString() + "";
shuju.ExcuteSql(strSql);
}
}
{
string xjg=this.TextBox1.Text;
string whereClause = string.Empty;
CheckBox chk;
foreach (GridViewRow r in GridView1.Rows)
{
chk = (CheckBox)r.FindControl("CheckBox1");
if (chk != null)
{
if (chk.Checked)
{
if (whereClause == string.Empty)
whereClause += "'" + GridView1.DataKeys[r.RowIndex].Value.ToString() + "'";
else
whereClause += ",'" + GridView1.DataKeys[r.RowIndex].Value.ToString() + "'";
}
}
}
if (whereClause != string.Empty)
{
SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=sa;database=pubs");
//假设你的gjid为数字类型字段
string strSQL = ""Update sc_gj set fy='" + xjg + "' where gjid in (" + whereClause + ")";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
//下面重新绑定GridView
BindGrid();
}
<asp:GridView id="GridView1" runat="server" DataKeyNames="gjid"
...
string strSQL = ""Update sc_gj set fy='" + xjg + "' where gjid in (" + whereClause + ")";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
这个才是最关键的部分;
SqlCommand是不能循环使用的,除非在dataread关闭后!
704174307,注明CSDN
没有值 还是为空 ~!我测试了
!page.isPostBack
有没有的啊
我的神啊
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string xjg=this.TextBox1.Text;
string idList="不可能的ID";
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox lb1 = (CheckBox)gr.FindControl("CheckBox1");
if (lb1.Checked=true)
{
idList+=",";
idList+=dtTable.Rows[0]["gjid"].ToString();
}
}
string strSql="update sc_gj set fy='"+xjg+"' where gjid in ("+idList+")";
shuju.ExcuteSql(strSql); }
不知道这样写对不对