ASP.NET实现GridView删除功能
2012-12-21 22:37
483 查看
public partial class _Default : System.Web.UI.Page { SqlDataAdapter sda = null; private void Bind() { string strConnectionString = "server=localhost;database=register;uid=sa;pwd=tianshi520;"; SqlConnection conn = new SqlConnection(strConnectionString); conn.Open(); string strSql = "select ID, Name as '留言者姓名', Content as '留言的信息', Time as '留言日期'" + " from msgboard"; sda = new SqlDataAdapter(strSql, conn); DataTable dt = new DataTable(); sda.Fill(dt); conn.Close(); gvMsg.DataKeyNames = new System.String[] { "ID" }; gvMsg.DataSource = dt; gvMsg.DataBind(); } protected void Page_Load(object sender, EventArgs e) { Bind(); } protected void sqlDS_Selecting(object sender, SqlDataSourceSelectingEventArgs e) { } protected void gvMsg_RowDeleting(object sender, GridViewDeleteEventArgs e) { string strSql = "delete from msgboard where ID = " + gvMsg.DataKeys[e.RowIndex].Value.ToString(); string strConnectionString = "server=localhost;database=register;uid=sa;pwd=tianshi520;"; SqlConnection conn = new SqlConnection(strConnectionString); conn.Open(); SqlCommand cmd = new SqlCommand(strSql, conn); cmd.ExecuteNonQuery(); conn.Close(); Bind(); } protected void gvMsg_RowCommand(object sender, GridViewCommandEventArgs e) { } protected void btnOk_Click(object sender, EventArgs e) { string strSql = string.Format("insert into msgboard(Name, Content, Time) values('{0}', '{1}', {2})", txtName.Text.Trim(), txtContent.Text.Trim(), DateTime.Now.ToShortDateString()); //Response.Write(strSql); string strConnectionString = "server=localhost;database=register;uid=sa;pwd=tianshi520;"; SqlConnection conn = new SqlConnection(strConnectionString); conn.Open(); SqlCommand cmd = new SqlCommand(strSql, conn); cmd.ExecuteNonQuery(); Bind(); } protected void gvMsg_RowEditing(object sender, GridViewEditEventArgs e) { gvMsg.EditIndex = e.NewEditIndex; }
相关文章推荐
- asp.net gridview实现全选,反选与删除记录的操作代码
- asp.net中弹出确认窗口(confirm),实现删除确认的功能
- ASP.NET中运用DataList控件来实现编辑、更新、删除、取消等功能
- asp.net 中利用GridView实现增加,修改,删除
- asp.net下gridview 批量删除的实现方法
- ASP.NET实现增删改查等功能(Access版)系统之四-删除
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)-.NET教程,Asp.Net开发
- Asp.net之使用GridView控件的RowCommand事件实现修改和删除
- asp.net 中GridView控件实现全选及反选的功能
- (分页、排序等)asp.net 2.0下一个标准GRIDVIEW功能的实现
- asp.net GridView中删除按钮的实现
- asp.net GridView增加删除功能
- 通过AJAX与ASP.NET结合实现的仿GridView增删改查功能!
- asp.net 2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- asp.net实现固定GridView标题栏的方法(冻结列功能)
- ASP.NET实现Cookie功能的三个基本操作(写入,读取,删除)
- ASP.NET—006:GridView删除一行JS实现
- Asp.net+Extjs实现对GridPanel分页、行内编辑、增加、删除、弹出新窗体增加、数据验证、编辑指定列、数据格式转换等功能
- asp.net 不用GridView自带删除功能,删除一行数据