代码如下:
private void editbun_Click(object sender, System.EventArgs e)
{
if(NTitle.Text.Trim().Length >0 && NContent.Text.Trim().Length > 0)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["sqlConnStr"]);SqlCommand myCommand = new SqlCommand("Update News Set Title='"+NTitle.Text+"',Body='"+NContent.Text+"' Where NewsID = '"+EditNewsID.ToString()+"'",myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Write("<script>alert('修改成功!');location.href='NewsList.aspx':</script>");
}
else
{
Response.Write("<script>alert('新闻标题或内容不能为空!');</script>");
}
}
private void editbun_Click(object sender, System.EventArgs e)
{
if(NTitle.Text.Trim().Length >0 && NContent.Text.Trim().Length > 0)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["sqlConnStr"]);SqlCommand myCommand = new SqlCommand("Update News Set Title='"+NTitle.Text+"',Body='"+NContent.Text+"' Where NewsID = '"+EditNewsID.ToString()+"'",myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Write("<script>alert('修改成功!');location.href='NewsList.aspx':</script>");
}
else
{
Response.Write("<script>alert('新闻标题或内容不能为空!');</script>");
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;namespace WebApplication1
{
/// <summary>
/// EditNews 的摘要说明。
/// </summary>
public class EditNews : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox NTitle;
protected System.Web.UI.WebControls.TextBox NContent;
protected System.Web.UI.WebControls.Button editbun;
public int EditNewsID=0;
public string ETitle,EBody; private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(Request.Params["ID"] != null)
{
EditNewsID = Int32.Parse(Request.Params["ID"].ToString());
}
if (!Page.IsPostBack)
{
if(EditNewsID > 0);
{
BindNewsInfo(EditNewsID);
}
}
} public void BindNewsInfo(int EditNewsID)
{
if (EditNewsID > 0)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["sqlConnStr"]); SqlCommand myCommand = new SqlCommand("Select Title,Body From [News] Where NewsID="+EditNewsID+"",myConnection); myConnection.Open(); SqlDataReader ds = myCommand.ExecuteReader();
while(ds.Read())
{
NTitle.Text = ds["Title"].ToString();
NContent.Text = ds["Body"].ToString();
}
ds.Close();
}
} private void editbun_Click(object sender, System.EventArgs e)
{
if(NTitle.Text.Trim().Length >0 && NContent.Text.Trim().Length > 0)
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["sqlConnStr"]); SqlCommand myCommand = new SqlCommand("Update News Set Title='"+NTitle.Text+"',Body='"+NContent.Text+"' Where NewsID = '"+EditNewsID.ToString()+"'",myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
Response.Write("<script>alert('修改成功!');location.href='NewsList.aspx':</script>");
}
else
{
Response.Write("<script>alert('新闻标题或内容不能为空!');</script>");
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.editbun.Click += new System.EventHandler(this.editbun_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}
<%@ Page language="c#" Codebehind="EditNews.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.EditNews" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>EditNews</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<table border="1" cellpadding="2" cellspacing="2" width="80%">
<tr>
<td>新闻标题:</td>
<td><asp:TextBox ID="NTitle" Runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>新闻内容:</td>
<td><asp:TextBox ID="NContent" Runat="server" Columns="35" Rows="9" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td align="center" colspan="2"><asp:Button id="editbun" Runat="server" Text="确定修改"></asp:Button></td>
</tr>
</table>
</form>
</TABLE></body>
</HTML>
{}
加这东西就OK了~~
if (!Page.IsPostBack)
{
if(EditNewsID > 0);
{
BindNewsInfo(EditNewsID);
}
}