ASP.NET实现GridView删除功能

简介:
 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;
        }

目录
相关文章
|
18天前
|
存储 文字识别 C#
.NET开源免费、功能强大的 Windows 截图录屏神器
今天大姚给大家分享一款.NET开源免费(基于GPL3.0开源协议)、功能强大、简洁灵活的 Windows 截图、录屏、Gif动图制作神器:ShareX。
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
140 0
|
2月前
|
C# Windows
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
|
2月前
|
SQL 开发框架 前端开发
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
34 0
|
2月前
|
SQL 开发框架 .NET
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
ASP.NET Web——GridView完整增删改查示例(全篇幅包含sql脚本)大二结业考试必备技能
33 0
|
7月前
|
Apache
基于commons-net实现ftp创建文件夹、上传、下载功能.
基于commons-net实现ftp创建文件夹、上传、下载功能.
106 0
|
9月前
|
移动开发 监控 网络协议
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
基于Socket通讯(C#)和WebSocket协议(net)编写的两种聊天功能(文末附源码下载地址)
|
5月前
|
开发框架 前端开发 .NET
用ajax和asp.net实现智能搜索功能
用ajax和asp.net实现智能搜索功能
43 0
|
9月前
|
SQL 安全 前端开发
.NET开源免费功能最全的商城项目
.NET开源免费功能最全的商城项目
|
9月前
|
开发框架 前端开发 JavaScript
WPF+ASP.NET SignalR实现简易在线聊天功能
WPF+ASP.NET SignalR实现简易在线聊天功能
130 0