您的位置:首页 > 编程语言

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

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();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐