62,050
社区成员
发帖
与我相关
我的任务
分享
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string storageid = this.DropDownList1.SelectedValue;
string yhr = this.DropYangHuRen.SelectedItem.Text;
string yhdt = this.txtyanghurq.Text;
string cmd = e.CommandName;
int i = Convert.ToInt32(e.CommandArgument);
if (cmd == "save")
{
string pbh = ((Label)GridView1.Rows[i].FindControl("labbm")).Text.ToString();
string pname = ((Label)GridView1.Rows[i].FindControl("labname")).Text.ToString();
string ppihao = ((Label)GridView1.Rows[i].FindControl("labpihao")).Text.ToString();
string pzhuce = ((Label)GridView1.Rows[i].FindControl("labzhuce")).Text.ToString();
string pxiaoqi = ((HiddenField)GridView1.Rows[i].FindControl("HiddenField1")).Value.ToString();
string pnum = ((Label)GridView1.Rows[i].FindControl("labnumber")).Text.ToString();
string punitname = ((Label)GridView1.Rows[i].FindControl("labdanwei")).Text.ToString();
string pspec = ((Label)GridView1.Rows[i].FindControl("labspec")).Text.ToString();
string pcompany = ((Label)GridView1.Rows[i].FindControl("labcompany")).Text.ToString();
string psupplier = ((Label)GridView1.Rows[i].FindControl("labsupplier")).Text.ToString();
string pcuoshi = ((TextBox)GridView1.Rows[i].FindControl("TextBox1")).Text.ToString();
string pbeizhu = ((TextBox)GridView1.Rows[i].FindControl("TextBox2")).Text.ToString();
string waiguan = ((DropDownList)GridView1.Rows[i].FindControl("DropWaiGuan")).SelectedItem.Text.ToString();
string baozhuang = ((DropDownList)GridView1.Rows[i].FindControl("DropBaoZhuang")).SelectedItem.Text.ToString();
string zhiliang = ((DropDownList)GridView1.Rows[i].FindControl("DropZhiLiang")).SelectedItem.Text.ToString();
string jieguo = ((DropDownList)GridView1.Rows[i].FindControl("DropResult")).SelectedItem.Text.ToString();
string sqlfield = "[storageid],[pcode],[p_title],[p_reg],[p_spec],[p_manufactor],[p_unit],[p_count],[p_pihao],[p_xiaoqi],[p_supplier],[waiguan],[baozhuang],[zhiliang],[cuoshi],[result],[beizhu],[yanghu_emp],[yanghu_dt]";
string sql = "insert into Gsp_YangHu(" + sqlfield + ")values('" + storageid + "','" + pbh + "','" + pname + "','" + pzhuce + "','" + pspec + "','" + pcompany + "','" + punitname + "','" + pnum + "','" + ppihao + "','" + pxiaoqi + "','" + psupplier + "','" + waiguan + "','" + baozhuang + "','" + zhiliang + "','" + pcuoshi + "','" + jieguo + "','" + pbeizhu + "','" + yhr + "','" + yhdt + "')";
Class1.ExecSql(sql);
this.WriteLog("添加器械养护记录成功");
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('添加成功,退出请点击下方【关闭该窗口】')", true);
}
}//RowCommand结尾
上述这段代码单独对当前行的插入操作没有任何问题,因为linkbutton在模板里面。
现在button1在gridview之外,所以就不好弄。因为这个不好控制所选行的索引了。具体这部分代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (this.GridView1.Rows.Count <= 0)
{
this.lblMessage.Visible = true;
this.lblMessage.Text = "请选择有商品的仓库";
return;
}
else
{
this.lblMessage.Visible = false;
}
string storageid = this.DropDownList1.SelectedValue;
string yhr = this.DropYangHuRen.SelectedItem.Text;
string yhdt = this.txtyanghurq.Text;
string str_check = "(";
string sql;
CheckBox cb;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
cb.Checked = true;
if (cb.Checked == true)
{
str_check = str_check + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
// str_check = str_check.Substring(0, str_check.Length - 1) + ")";
string pbh = ((Label)GridView1.Rows[i].FindControl("labbm")).Text.ToString();
string pname = ((Label)GridView1.Rows[i].FindControl("labname")).Text.ToString();
string ppihao = ((Label)GridView1.Rows[i].FindControl("labpihao")).Text.ToString();
string pzhuce = ((Label)GridView1.Rows[i].FindControl("labzhuce")).Text.ToString();
string pxiaoqi = ((HiddenField)GridView1.Rows[i].FindControl("HiddenField1")).Value.ToString();
string pnum = ((Label)GridView1.Rows[i].FindControl("labnumber")).Text.ToString();
string punitname = ((Label)GridView1.Rows[i].FindControl("labdanwei")).Text.ToString();
string pspec = ((Label)GridView1.Rows[i].FindControl("labspec")).Text.ToString();
string pcompany = ((Label)GridView1.Rows[i].FindControl("labcompany")).Text.ToString();
string psupplier = ((Label)GridView1.Rows[i].FindControl("labsupplier")).Text.ToString();
string pcuoshi = ((TextBox)GridView1.Rows[i].FindControl("TextBox1")).Text.ToString();
string pbeizhu = ((TextBox)GridView1.Rows[i].FindControl("TextBox2")).Text.ToString();
string waiguan = ((DropDownList)GridView1.Rows[i].FindControl("DropWaiGuan")).SelectedItem.Text.ToString();
string baozhuang = ((DropDownList)GridView1.Rows[i].FindControl("DropBaoZhuang")).SelectedItem.Text.ToString();
string zhiliang = ((DropDownList)GridView1.Rows[i].FindControl("DropZhiLiang")).SelectedItem.Text.ToString();
string jieguo = ((DropDownList)GridView1.Rows[i].FindControl("DropResult")).SelectedItem.Text.ToString();
//去掉最后的逗号,并且加上右手号 ★示例★
// str_check = str_check.Substring(0, str_check.Length - 1) + ")";
//if noexists (select * from table1 where mid='aa') INSERT INTO table1(mid,name,msg) VALUES('aa','bb','cc')
string sqlfield = "[storageid],[pcode],[p_title],[p_reg],[p_spec],[p_manufactor],[p_unit],[p_count],[p_pihao],[p_xiaoqi],[p_supplier],[waiguan],[baozhuang],[zhiliang],[cuoshi],[result],[beizhu],[yanghu_emp],[yanghu_dt]";
sql = "insert into Gsp_YangHu(" + sqlfield + ")values('" + storageid + "','" + pbh + "','"+pname+"','" + pzhuce + "','" + pspec + "','" + pcompany + "','" + punitname + "','" + pnum + "','" +ppihao + "','" + pxiaoqi + "','" + psupplier + "','" + waiguan + "','" +baozhuang + "','" + zhiliang + "','" + pcuoshi + "','"+jieguo+"','"+pbeizhu+"','"+yhr+"','"+yhdt+"')";
Class1.ExecSql(sql);
}
else
{
cb.Checked = false;
}
}
this.WriteLog("添加器械养护记录成功");
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('添加成功,退出请点击下方【关闭该窗口】')", true);
}
catch(Exception exc)
{
this.lblMessage.Visible = true;
this.lblMessage.Text = exc.ToString();
}
}
}
[/quote]
cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
cb.Checked = true;
你这块为什么直接复制为True,这样的话你的条件一直是成立的,把cb.Checked=true取消掉 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string storageid = this.DropDownList1.SelectedValue;
string yhr = this.DropYangHuRen.SelectedItem.Text;
string yhdt = this.txtyanghurq.Text;
string cmd = e.CommandName;
int i = Convert.ToInt32(e.CommandArgument);
if (cmd == "save")
{
string pbh = ((Label)GridView1.Rows[i].FindControl("labbm")).Text.ToString();
string pname = ((Label)GridView1.Rows[i].FindControl("labname")).Text.ToString();
string ppihao = ((Label)GridView1.Rows[i].FindControl("labpihao")).Text.ToString();
string pzhuce = ((Label)GridView1.Rows[i].FindControl("labzhuce")).Text.ToString();
string pxiaoqi = ((HiddenField)GridView1.Rows[i].FindControl("HiddenField1")).Value.ToString();
string pnum = ((Label)GridView1.Rows[i].FindControl("labnumber")).Text.ToString();
string punitname = ((Label)GridView1.Rows[i].FindControl("labdanwei")).Text.ToString();
string pspec = ((Label)GridView1.Rows[i].FindControl("labspec")).Text.ToString();
string pcompany = ((Label)GridView1.Rows[i].FindControl("labcompany")).Text.ToString();
string psupplier = ((Label)GridView1.Rows[i].FindControl("labsupplier")).Text.ToString();
string pcuoshi = ((TextBox)GridView1.Rows[i].FindControl("TextBox1")).Text.ToString();
string pbeizhu = ((TextBox)GridView1.Rows[i].FindControl("TextBox2")).Text.ToString();
string waiguan = ((DropDownList)GridView1.Rows[i].FindControl("DropWaiGuan")).SelectedItem.Text.ToString();
string baozhuang = ((DropDownList)GridView1.Rows[i].FindControl("DropBaoZhuang")).SelectedItem.Text.ToString();
string zhiliang = ((DropDownList)GridView1.Rows[i].FindControl("DropZhiLiang")).SelectedItem.Text.ToString();
string jieguo = ((DropDownList)GridView1.Rows[i].FindControl("DropResult")).SelectedItem.Text.ToString();
string sqlfield = "[storageid],[pcode],[p_title],[p_reg],[p_spec],[p_manufactor],[p_unit],[p_count],[p_pihao],[p_xiaoqi],[p_supplier],[waiguan],[baozhuang],[zhiliang],[cuoshi],[result],[beizhu],[yanghu_emp],[yanghu_dt]";
string sql = "insert into Gsp_YangHu(" + sqlfield + ")values('" + storageid + "','" + pbh + "','" + pname + "','" + pzhuce + "','" + pspec + "','" + pcompany + "','" + punitname + "','" + pnum + "','" + ppihao + "','" + pxiaoqi + "','" + psupplier + "','" + waiguan + "','" + baozhuang + "','" + zhiliang + "','" + pcuoshi + "','" + jieguo + "','" + pbeizhu + "','" + yhr + "','" + yhdt + "')";
Class1.ExecSql(sql);
this.WriteLog("添加器械养护记录成功");
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('添加成功,退出请点击下方【关闭该窗口】')", true);
}
}//RowCommand结尾
上述这段代码单独对当前行的插入操作没有任何问题,因为linkbutton在模板里面。
现在button1在gridview之外,所以就不好弄。因为这个不好控制所选行的索引了。具体这部分代码如下:
protected void Button1_Click(object sender, EventArgs e)
{
try
{
if (this.GridView1.Rows.Count <= 0)
{
this.lblMessage.Visible = true;
this.lblMessage.Text = "请选择有商品的仓库";
return;
}
else
{
this.lblMessage.Visible = false;
}
string storageid = this.DropDownList1.SelectedValue;
string yhr = this.DropYangHuRen.SelectedItem.Text;
string yhdt = this.txtyanghurq.Text;
string str_check = "(";
string sql;
CheckBox cb;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
cb.Checked = true;
if (cb.Checked == true)
{
str_check = str_check + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";
// str_check = str_check.Substring(0, str_check.Length - 1) + ")";
string pbh = ((Label)GridView1.Rows[i].FindControl("labbm")).Text.ToString();
string pname = ((Label)GridView1.Rows[i].FindControl("labname")).Text.ToString();
string ppihao = ((Label)GridView1.Rows[i].FindControl("labpihao")).Text.ToString();
string pzhuce = ((Label)GridView1.Rows[i].FindControl("labzhuce")).Text.ToString();
string pxiaoqi = ((HiddenField)GridView1.Rows[i].FindControl("HiddenField1")).Value.ToString();
string pnum = ((Label)GridView1.Rows[i].FindControl("labnumber")).Text.ToString();
string punitname = ((Label)GridView1.Rows[i].FindControl("labdanwei")).Text.ToString();
string pspec = ((Label)GridView1.Rows[i].FindControl("labspec")).Text.ToString();
string pcompany = ((Label)GridView1.Rows[i].FindControl("labcompany")).Text.ToString();
string psupplier = ((Label)GridView1.Rows[i].FindControl("labsupplier")).Text.ToString();
string pcuoshi = ((TextBox)GridView1.Rows[i].FindControl("TextBox1")).Text.ToString();
string pbeizhu = ((TextBox)GridView1.Rows[i].FindControl("TextBox2")).Text.ToString();
string waiguan = ((DropDownList)GridView1.Rows[i].FindControl("DropWaiGuan")).SelectedItem.Text.ToString();
string baozhuang = ((DropDownList)GridView1.Rows[i].FindControl("DropBaoZhuang")).SelectedItem.Text.ToString();
string zhiliang = ((DropDownList)GridView1.Rows[i].FindControl("DropZhiLiang")).SelectedItem.Text.ToString();
string jieguo = ((DropDownList)GridView1.Rows[i].FindControl("DropResult")).SelectedItem.Text.ToString();
//去掉最后的逗号,并且加上右手号 ★示例★
// str_check = str_check.Substring(0, str_check.Length - 1) + ")";
//if noexists (select * from table1 where mid='aa') INSERT INTO table1(mid,name,msg) VALUES('aa','bb','cc')
string sqlfield = "[storageid],[pcode],[p_title],[p_reg],[p_spec],[p_manufactor],[p_unit],[p_count],[p_pihao],[p_xiaoqi],[p_supplier],[waiguan],[baozhuang],[zhiliang],[cuoshi],[result],[beizhu],[yanghu_emp],[yanghu_dt]";
sql = "insert into Gsp_YangHu(" + sqlfield + ")values('" + storageid + "','" + pbh + "','"+pname+"','" + pzhuce + "','" + pspec + "','" + pcompany + "','" + punitname + "','" + pnum + "','" +ppihao + "','" + pxiaoqi + "','" + psupplier + "','" + waiguan + "','" +baozhuang + "','" + zhiliang + "','" + pcuoshi + "','"+jieguo+"','"+pbeizhu+"','"+yhr+"','"+yhdt+"')";
Class1.ExecSql(sql);
}
else
{
cb.Checked = false;
}
}
this.WriteLog("添加器械养护记录成功");
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('添加成功,退出请点击下方【关闭该窗口】')", true);
}
catch(Exception exc)
{
this.lblMessage.Visible = true;
this.lblMessage.Text = exc.ToString();
}
}
}