数据在后台修改 删除 更新 正确代码(共享)

本文详细介绍了如何通过更新代码实现网页上数据的高效更新与管理,包括数据的增加、删除、修改等功能,以及如何在不同的数据操作事件中进行响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="update2.aspx.cs" Inherits="update2" EnableViewState="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            OnPageIndexChanging="GridView1_PageIndexChanging1" OnRowEditing="GridView1_RowEditing"
            OnSelectedIndexChanging="GridView1_SelectedIndexChanging" PageSize="5" DataKeyNames="newsid,newstitle,tj" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" OnRowDeleting="GridView1_RowDeleting">
            <Columns>
                <asp:BoundField DataField="newstitle" HeaderText="新闻标题" />
                <asp:BoundField DataField="tj" HeaderText="推荐" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowSelectButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>

 

 

 

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class update2 : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcon"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
        getda();
    }

    protected void getda()
    {
        SqlCommand cmd = new SqlCommand("select * from lxnews", conn);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "lxnews");
        this.GridView1.DataSource = ds.Tables[0].DefaultView;
        this.GridView1.DataBind();
    }
    protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        getda();
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        this.GridView1.EditIndex = e.NewEditIndex;
        getda();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        getda();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
            string newid = this.GridView1.DataKeys[e.RowIndex][0].ToString();
            string lxtitle = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text.ToString();//可以在此设置断点试一下
            string lxtj = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
            //Response.Write(newid + "-----" + lxtitle);
            //Response.End();
            updatenews(newid, lxtitle, lxtj);
            this.GridView1.EditIndex = -1;
            getda();
    }
    public void updatenews(string newsid, string newstitle, string newstj)
    {
        SqlCommand cmd = new SqlCommand("update lxnews set newstitle='" + newstitle + "',tj='" + newstj + "' where newsid=" + Convert.ToInt32(newsid) + "", conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string newid = this.GridView1.DataKeys[e.RowIndex][0].ToString();
        SqlCommand cmd = new SqlCommand("delete from lxnews where newsid=" + Convert.ToInt32(newid) + "", conn);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        getda();
    }
 
}

 

 

 

更改后 的代码  及效果     //更新为更新第一次 id赋值给 username  更新第二次 id赋值给 pwd   根本无法更真实文档

 

 

已经实现成功  pageload   必须加入 if(!ispostback){ 

}

 

前台页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication75._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            OnPageIndexChanging="GridView1_PageIndexChanging1" OnRowEditing="GridView1_RowEditing"
            OnSelectedIndexChanging="GridView1_SelectedIndexChanging" PageSize="5" DataKeyNames="id,username,pwd" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowUpdating="GridView1_RowUpdating" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" GridLines="Horizontal" OnRowDeleting="GridView1_RowDeleting">
            <Columns>
                <asp:BoundField DataField="id" HeaderText="id" />
                <asp:BoundField DataField="username" HeaderText="用户名" />
                <asp:BoundField DataField="pwd" HeaderText="密码" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowSelectButton="True" />
                <asp:CommandField ShowDeleteButton="True" />
            </Columns>
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <RowStyle BackColor="White" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
   
    </div>
    </form>
</body>
</html>

 

 

 

 

 

后台

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication75
{
   //更新为更新第一次 id赋值给 username  更新第二次 id赋值给 pwd   根本无法更真实文档

    public partial class _Default : System.Web.UI.Page
    {
        //string strconn = "server=.;database=admin;integrated security=true;";
        //SqlConnection conn = new SqlConnection(strconn);
        protected void Page_Load(object sender, EventArgs e)
        {
            getda();
        }

        protected void getda()
        {
            string strconn = "server=.;database=admin;integrated security=true;";
            SqlConnection conn = new SqlConnection(strconn);
            SqlCommand cmd = new SqlCommand("select * from admin", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds, "admin");
            this.GridView1.DataSource = ds.Tables[0].DefaultView;
            this.GridView1.DataBind();
        }
        protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            getda();
        }
        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.GridView1.EditIndex = e.NewEditIndex;
            getda();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.GridView1.EditIndex = -1;
            getda();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = this.GridView1.DataKeys[e.RowIndex][0].ToString();
            string username = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();//可以在此设置断点试一下
            string pwd = ((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
            //Response.Write(newid + "-----" + lxtitle);
            //Response.End();
            updatenews(id, username , pwd );
            this.GridView1.EditIndex = -1;
            getda();
        }
        public void updatenews(string id, string username, string pwd)
        {
            string strconn = "server=.;database=admin;integrated security=true;";
            SqlConnection conn = new SqlConnection(strconn);
            SqlCommand cmd = new SqlCommand("update admin set username='" + username + "',pwd='" + pwd + "' where id=" + Convert.ToInt32(id) + "", conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string strconn = "server=.;database=admin;integrated security=true;";
            SqlConnection conn = new SqlConnection(strconn);
            string id = this.GridView1.DataKeys[e.RowIndex][0].ToString();
            SqlCommand cmd = new SqlCommand("delete from admin where id=" + Convert.ToInt32(id) + "", conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();
            getda();
        }
    }
}

 

熟练使用servlet实现CRUD 1.通过request的转发携带数据,实现后台集合里所有元素的页面显示(全查) 提示:可以不用登录,可以直接访问findAllController, 得到数据后,直接转发到index.jsp页面 2.通过前端传值和后台处理,用List集合模拟数据库实现添加功能并更新页面数据显示,参考流程如下: a.前端页面通过form表单传值到后台 b.后台通过request接收前端传值 c.后台通过静态集合模拟数据库添加数据 d.数据添加完成后,通过request的setAttribute(k,v)方法,把数据转发到index.jsp页面 e.index.jsp页面接收到request发来的数据,并显示 3.通过前端传值和后台处理,结合request两次转发,用List集合模拟数据库实现修改功能 a.前端页面通过JS代码(location.href=”URL”+参数)传值到后台 b.后台通过request接收前端传值,并组装成对象,转发到updateShow.jsp页面 c.updateShow.jsp页面接收到对象后显示到页面 d.用户通过udpateShow.jsp页面输入修改后的数据并提交表单到updateConfirm的servlet e.后台通过静态集合模拟数据库修改数据 f.数据修改完成后,通过request的setAttribute(k,v)方法,把数据转发到index.jsp页面 g.index.jsp页面接收到request发来的数据,并显示 4.通过简单JS代码,实现List模拟数据库删除数据功能并刷新表格 a.前端页面通过JS代码(location.href=”URL”…)传参到后台 b.后台通过request接收前端传值 c.后台通过静态集合模拟数据库删除数据 d.数据删除完成后,通过request的setAttribute(k,v)方法,把数据转发到index.jsp页面 index.jsp页面接收到request发来的数据,并显示
最新发布
05-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值