我做了一个新闻更新页面,可以正常的读出来,但是在修改了Textbox里面的值以后 在更新数据库的时候读到的Textbox里面的值 还是当初从数据库取出来的值 不是修改后的 代码如下 希望高手给指点一下。using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace jnsapo
{
/// <summary>
/// UpdataNews 的摘要说明。
/// </summary>
public class UpdataNews : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
public OleDbConnection conn;
public OleDbCommand updatecmd;
protected OleDbDataReader dr;
public string text,id,title,image, content,type;
protected System.Web.UI.WebControls.TextBox TextBox3;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
id = Request.QueryString["ID"].ToString();
String strconn = "Provider=Microsoft.Jet.OleDb.4.0;Mode=ReadWrite;Jet OLEDB:Database Password=***;Data Source=";
strconn+=Server.MapPath("mdb/jnsapo.mdb");
try
{
conn = new OleDbConnection(strconn);
conn.Open();
OleDbCommand selectcmd = new OleDbCommand("select * from News where id ="+id+" ",conn);
dr =selectcmd.ExecuteReader();
dr.Read();
title = dr["title"].ToString();
image = dr["imagee"].ToString();
content =dr["content"].ToString();
type =dr["type"].ToString();
dr.Close();
TextBox1.Text = title;
TextBox2.Text = image;
TextBox3.Text = content;
DropDownList1.SelectedValue = type;
}
catch(Exception)
{
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.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button2_Click(object sender, System.EventArgs e)
{
Response.Redirect("NewsManage.aspx");
} private void Button1_Click(object sender, System.EventArgs e)
{
title = TextBox1.Text.ToString();
image = TextBox2.Text.ToString();
content = TextBox3.Text.ToString();
type = DropDownList1.SelectedValue.ToString();
OleDbCommand updatecmd = new OleDbCommand("update News set title = '"+title+"' where id ="+id+" ",conn);
try
{
updatecmd.ExecuteNonQuery();
Response.Write("<script>alert('更新成功!')</script>");
}
catch(Exception)
{
Response.Write("<script>alert('好像出问题了 找管理员联系一下吧。')</script>");
}
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace jnsapo
{
/// <summary>
/// UpdataNews 的摘要说明。
/// </summary>
public class UpdataNews : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
public OleDbConnection conn;
public OleDbCommand updatecmd;
protected OleDbDataReader dr;
public string text,id,title,image, content,type;
protected System.Web.UI.WebControls.TextBox TextBox3;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
id = Request.QueryString["ID"].ToString();
String strconn = "Provider=Microsoft.Jet.OleDb.4.0;Mode=ReadWrite;Jet OLEDB:Database Password=***;Data Source=";
strconn+=Server.MapPath("mdb/jnsapo.mdb");
try
{
conn = new OleDbConnection(strconn);
conn.Open();
OleDbCommand selectcmd = new OleDbCommand("select * from News where id ="+id+" ",conn);
dr =selectcmd.ExecuteReader();
dr.Read();
title = dr["title"].ToString();
image = dr["imagee"].ToString();
content =dr["content"].ToString();
type =dr["type"].ToString();
dr.Close();
TextBox1.Text = title;
TextBox2.Text = image;
TextBox3.Text = content;
DropDownList1.SelectedValue = type;
}
catch(Exception)
{
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.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button2_Click(object sender, System.EventArgs e)
{
Response.Redirect("NewsManage.aspx");
} private void Button1_Click(object sender, System.EventArgs e)
{
title = TextBox1.Text.ToString();
image = TextBox2.Text.ToString();
content = TextBox3.Text.ToString();
type = DropDownList1.SelectedValue.ToString();
OleDbCommand updatecmd = new OleDbCommand("update News set title = '"+title+"' where id ="+id+" ",conn);
try
{
updatecmd.ExecuteNonQuery();
Response.Write("<script>alert('更新成功!')</script>");
}
catch(Exception)
{
Response.Write("<script>alert('好像出问题了 找管理员联系一下吧。')</script>");
}
}
}
}
if(Page.isPostback)
{
......
}
在Page_Load里加上
if(!Page.isPostback)
{
......
}
if(!IsPostBack)
{
....
}
{
....
}
加了就ok了