关于checkbox多选一起提交问题!!!
在开发的过程中我们常常会用到checkbox 有可能会连续点击两个进行提交,通常我们的做法是一条一条的提交,这样来回操作数据库,时间会很长,我们可以放到一起提交上去。
// 红色字体部分就是如何提交的写法。。。。
protected void btnbc_Click(object sender, EventArgs e) { string dh = ddlpddh.SelectedValue.ToString(); string sql2 = string.Empty; System.Data.DataTable dt = dbgr.GetDataTable(sql);
StringBuilder sb = new StringBuilder();//插入 开始。。。。先定义一个StringBuilder StringBuilder de = new StringBuilder();//删除 开始 sb.Append("begin "); de.Append("begin ");
for (int i = 0; i < GridView1.Rows.Count; i++) { tbyy = (System.Web.UI.WebControls.TextBox)this.GridView1.Rows[i].FindControl("tbyy"); if (tbyy.Text.ToString() != "") { string rwdh = dt.Rows[i]["rwdh"].ToString(); string jlsj = dt.Rows[i]["jlsj"].ToString(); string dw = ViewState["dw"].ToString(); string zcbm = dt.Rows[i]["zcbm"].ToString(); string zcmc = dt.Rows[i]["zcmc"].ToString(); string ggxh = dt.Rows[i]["ggxh"].ToString(); string jldw = dt.Rows[i]["jldw"].ToString(); string fhsl = dt.Rows[i]["fhsl"].ToString(); string zzcj = dt.Rows[i]["zzcj"].ToString(); string tcrq = dt.Rows[i]["tcrq"].ToString(); string cph = dt.Rows[i]["cpjh"].ToString(); string zydw = dt.Rows[i]["dwmc"].ToString(); string yz = dt.Rows[i]["yz"].ToString(); string jz = dt.Rows[i]["jz"].ToString(); var li = ViewState["li"] as List<string>;//取LI中的值 for (int j = 0; j <li.Count; j++)//这个语句判断ZCBM是否插入进去过,如果有 就不在插入语句。 { if (zcbm==li [j]) { sql2 = "update zc_pdsj_hs set hsrq='" + tbyy.Text.ToString() + "' where zcbm='" + zcbm + "';"; } else { sql2 = "insert into zc_pdsj_hs(rwdh,rq,DW,zcbm,zcmc,ggxh,jldw,fhsl,zzcj,tcrq,cpjh,zydw,yz,jz,HSRQ,dwbm)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}'); "; sql2 = string.Format(sql2, rwdh, jlsj, dw, zcbm, zcmc, ggxh, jldw, fhsl, zzcj, tcrq, cph, zydw, yz, jz, tbyy.Text.ToString(), ViewState["dwbm"].ToString()); } } sb.Append(sql2);//把语句放到刚才定义的sb中 string del = "delete from zc_pdsj where rwdh = '" + rwdh + "'and zcbm='" + zcbm + "';"; de.Append(del); } else { //页面无刷新弹框 Page.RegisterStartupScript("", "<script>alert('请输入毁损时间!')</script>"); return; } try { sb.Append("end;");//插入结束 de.Append("end;");//删除结束 this.dbgr.ExecSQL(sb.ToString());//数据库连接字符串。。。。。所有循环结束,并执行相应的操作。 this.dbgr.ExecSQL(de.ToString()); Response.Write("<script language=javascript>alert('提交成功!'); location='PKMX.aspx'</script>"); } catch (Exception ex) { Response.Write("<script language=javascript>alert('出现 " + ex + " 错误'); </script>"); } } }