数据在后台修改 删除 更新 正确代码(共享)
2011-11-18 09:44
441 查看
<%@ 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();
}
}
}
<!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();
}
}
}
相关文章推荐
- 关于数据在后台的删除修改(代码有错误等待更正 不完全)
- 对于分页视图里面修改数据,在DAL层提示存储区更新、插入和删除语句影响到了意外的行数(0)的原因
- webservice接口实现数据共享的实现的初步解决方案(更新、删除)
- sql生成(插入、修改、删除数据的存储过程)代码的存储过程
- Uva 10596 - Morning Walk 欧拉回路基础水题 并查集实现【uva数据修改,代码已更新】
- openerp学习笔记 对象调用(创建、修改),用于后台代码创建和更新对象
- JS调用C#后台代码---JS实现DataGrid“全选”、“反选”、调用后台代码批量删除数据
- Silverlight 3 关于数据添加,删除,修改,条件查询的一点代码
- 数据库修改之更新员工表,根据ID删除Mapping表数据,再插入数据
- ASP.NET MVC4 新手入门教程特别篇之一----Code First Migrations更新数据库结构(数据迁移)修改Entity FrameWork 数据结构(不删除数据)
- 【T-MAC学习笔记10之--数据修改之插入数据,更新数据,删除数据】
- js动态添加删除,后台取数据(示例代码)
- python中集合类型创建、更新、删除、修改以及操作符使用实例代码
- 高度封装FMDB框架:各用一句代码更新(添加&修改),查询,删除用户信息
- JS调用C#后台代码---JS实现DataGrid“全选”、“反选”、调用后台代码批量删除数据
- 七、SQL数据库,代码实现创建表,添加列,修改数据,删除数据
- 学生管理系统,用Java代码连接数据实现数据的添加、更新、删除操作
- 金蝶EAS,代码实现编辑界面EditUI数据不允许修改、删除
- c#.net2005向sql添加、更新、删除数据的原代码(winform)
- SQL 数据更新(插入数据,修改数据,删除数据) 实例_(学生,课程表,选修表)