数据在后台修改 删除 更新 正确代码(共享)
来源:互联网 发布:matlab保存数组到文件 编辑:程序博客网 时间:2024/05/05 02:27
<%@ 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();
}
}
}
- 数据在后台修改 删除 更新 正确代码(共享)
- 关于数据在后台的删除修改(代码有错误等待更正 不完全)
- (模板修改功能)在后台直接修改代码
- .NET的GridView在后台删除数据
- WPF中,怎么在后台代码进行数据绑定
- 国庆节在家 - 工作 (在后台修改job 中的script)
- 在后台(cs)代码中更改页面元素样式
- StringBuilder 在后台动态输出 html 代码 (博客园 )
- 三种方式使得iOS应用能够在后台进行数据更新和下载
- 三种方式使得iOS应用能够在后台进行数据更新和下载
- 三种方式使得iOS应用能够在后台进行数据更新和下载
- 三种方式使得iOS应用能够在后台进行数据更新和下载
- GridView手动在后台增加-删除(1)
- GridView手动在后台增加-删除(2)
- Mongodb 实现数据自动在后台删除--根据一个截止时间
- 在后台怎样修改数据源的"whereClause"
- 在后台里修改配置文件(web.config)
- 在后台修改前台模板文件
- 身体恢复
- vs2008下使用svn
- 找回忘记了的Windows XP系统登录密码
- HashMap和Hashtable的区别
- Oracle 查询并删除重复记录的SQL语句
- 数据在后台修改 删除 更新 正确代码(共享)
- PL/SQL DEVELOPER 基本用法详解(转)
- 敏捷开发般若敏捷系列之四:如何推广敏捷(上)(无我,无人,无众生)
- How to create a Gantt chart in SharePoint
- ClassFactory
- POI中的手动公式计算方法
- emacs 执行数学计算函数fib
- 重复数据删除和SSD的互补方法
- HDOJ 1005 的一些分析的摘要