c# textbox 问题 跪求

来源:百度知道 编辑:UC知道 时间:2024/05/14 03:20:55
主要功能是从数据库里读取数据,填到textbox中,如果不喜欢这数据,直接在TextBox中更改,然后按确定提交,但发现更新不了,跪求原因
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 admin_up : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
aa();

}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["jiayuanConnectionString"].ConnectionString);
cn.Open();
SqlCommand cm = new SqlCommand("UPDATE myDATA SET data='"+this.TextBox1.Text+"' where id='22'", cn);
cm.

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack){
aa();
}

}

你必须要使用Page.IsPostBack,1楼那样子.
这个的原因是,当你提交数据的时候页面重新加载,但是此时并没有执行你的事件,当加载完成,textbox里面的数据就变成数据库里面的数据了,这个你去MSDN上面,页面加载和事件处理方面的内容有涉及.

protected void Page_Load(..)
if(!this.page.IsPostBack)
{
aa();
}

IsPostBack属性是检测页面是否从服务器回发,即检测是否是第二次加载该页面。如果是第二次加载页面,返回false,所以要对该属性进行取反(!)操作。