执行命令方法的封装(添加/修改/删除)
2013-03-19 00:44
453 查看
View Code
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; //引入命名空间 using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { SqlDb db = new SqlDb(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindData();//调用自定义方法绑定数据到控件 } } public void BindData() { string sqlstr = "select * from bccd01 order by BccdID desc";//定义执行查询操作的SQL语句 db.BindData(GridView1, sqlstr); if (GridView1.Rows.Count > 0) { return;//有数据,不要处理 } else//显示表头并显示没有数据的提示信息 { StrHelper.GridViewHeader(GridView1); } } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { //定义一个整型变量count存储根据当前行索引获取到的指定单元格中的字段值 int count = int.Parse(GridView1.Rows[e.RowIndex].Cells[3].Text.Trim()); if (count > 0) { e.Cancel = true;//取消删除动作 lblMessage.Text = "警告:库存量大于0的商品不得删除!"; lblMessage.ForeColor = System.Drawing.Color.Red; return; } string delete_sql = "delete from bccd01 where BccdID='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'"; bool delete = db.ExceSQL(delete_sql);//调用ExceSQL执行删除操作 if (delete) { StrHelper.Alert("删除成功!"); BindData();//调用自定义方法重新绑定控件中数据 } else { StrHelper.Alert("删除失败!"); } } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //取得编辑行的关键字段的值 string bccdID = GridView1.DataKeys[e.RowIndex].Value.ToString(); //取得文本框中输入的内容 string bccdName=((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim(); string bccdPrice=((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim(); string bccdInStock=((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim(); //定义更新操作的SQL语句 string update_sql = "update bccd01 set BccdName='" + bccdName + "',BccdPrice='" + bccdPrice + "',BccdInStock='" + bccdInStock + "' where BccdID='" + bccdID + "'"; bool update = db.ExceSQL(update_sql);//调用ExceSQL执行更新操作 if (update) { StrHelper.Alert("修改成功!"); //设置GridView控件的编辑项的索引为-1,即取消编辑 GridView1.EditIndex = -1; BindData(); } else { StrHelper.Alert("修改失败!"); } } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { //设置GridView控件的编辑项的索引为-1,即取消编辑 GridView1.EditIndex = -1; BindData();//数据绑定 } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; BindData();//数据绑定 } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; BindData();//数据绑定 } protected void btnInsert_Click(object sender, EventArgs e) { string InsertSql = "Insert Into bccd01(BccdName,BccdPrice,BccdInStock) values('" + txtName.Text + "','" + txtPrice.Text + "','" + txtSum.Text + "')"; bool insert = db.ExceSQL(InsertSql);//调用公共类中的ExceSQL()执行添加操作 if (insert)//如果添加成功 { StrHelper.Alert("添加成功!"); BindData();//重新绑定数据 } else { StrHelper.Alert("添加失败!"); } } }
相关文章推荐
- 执行命令方法的封装(添加/修改/删除)
- linux命令详解之useradd命令使用方法[linux下 添加用户、删除用户、修改用户密码、用户组管理]
- 程序执行修改,添加,删除时出现程序停止的问题----原因事务问题
- web测试(输入框、添加和修改、删除功能)方法总结
- 高度封装FMDB框架:各用一句代码更新(添加&修改),查询,删除用户信息
- SQL语句的添加、删除、修改多种方法
- 给jqGrid数据行添加修改和删除操作链接(可以执行)
- Oracle 添加用户并赋权,修改密码,解锁,删除用户的方法
- javascript动态添加、修改、删除对象的属性和方法
- U-Boot添加menu命令的方法及U-Boot命令执行过程
- Myeclipse的使用方法-添加,修改,删除JRE
- SQL语句的添加、删除、修改多种方法
- SQL语句的添加、删除、修改多种方法
- Linux命令(9):添加,删除,修改,查看用户和用户组
- 如何管理和记录 SSIS 各个 Task 的开始执行时间和结束时间以及 Task 中添加|删除|修改的记录数
- 添加,查询,修改,删除测试方法及常见bug说明
- C# 添加、修改以及删除Excel迷你图表的方法
- U-Boot添加命令的方法及U-Boot命令执行过程
- python2.7 执行cmd命令及shell命令封装方法
- 精细化(添加 修改 删除 查看)用户权限分配方法