我用服务器端TEXTBOX 获取了 从数据库中读取的值 然后在此值上做修改 再通过 button 点击事件UPDATE新数据
可以点击按钮事件中获取TEXTBOX中的数据并不是修改后的数据 而是老数据 很奇怪 请教一下怎么解决
 protected void Page_Load(object sender, EventArgs e)
    {
        Session["newid"] = Request["id"];
        int i = Convert.ToInt32(Session["newid"]);        Conn myconn = new Conn();
        string sql = "SELECT * FROM newInfo WHERE ID =" + i;
        DataSet ds = myconn.myds(sql);
        this.txtTitle.Text = ds.Tables[0].Rows[0][1].ToString();
        this.Content.Text = ds.Tables[0].Rows[0][3].ToString();
        ds.Clear();    }    protected void submit_Click(object sender, EventArgs e)
    {
        Response.Write(txtTitle.Text);
        string tt = txtTitle.Text.Trim();
        string info = Content.Text;
        Conn myconn = new Conn();
        string sql = "UPDATE newInfo SET title = '" + tt + "',intro = '" + info + "' WHERE ID =  " + Convert.ToInt32(Session["newid"]);
        myconn.ExecSql(sql);
        //Response.Write(sql);
        this.lblText.Text = "修改成功";
    }

解决方案 »

  1.   

    这个页面的生命周期的知识相关。在你点击修改的时候,先触发的是page_load时间,所以页面呈现的是老数据。然后再触发修改时间。这时候,数据库中的数据实际已经修改了,而页面还未修改。应该这样修改
    protected void Page_Load(object sender, EventArgs e)
      {
         if(!IsPostBack)
          {
             BindData();
         }
      }
    private void BindData()
    {
      Session["newid"] = Request["id"];
      int i = Convert.ToInt32(Session["newid"]);  Conn myconn = new Conn();
      string sql = "SELECT * FROM newInfo WHERE ID =" + i;
      DataSet ds = myconn.myds(sql);
      this.txtTitle.Text = ds.Tables[0].Rows[0][1].ToString();
      this.Content.Text = ds.Tables[0].Rows[0][3].ToString();
      ds.Clear();
    }
    protected void submit_Click(object sender, EventArgs e)
      {
      Response.Write(txtTitle.Text);
      string tt = txtTitle.Text.Trim();
      string info = Content.Text;
      Conn myconn = new Conn();
      string sql = "UPDATE newInfo SET title = '" + tt + "',intro = '" + info + "' WHERE ID = " + Convert.ToInt32(Session["newid"]);
      myconn.ExecSql(sql);
      //Response.Write(sql);
      this.lblText.Text = "修改成功";
      BindData();
      }
    if (!IsPostBack)中的代码的意思是只在首次加载的时候执行的代码,当点击修改按钮之后,先执行page_load中的代码,因为是回发。所以不重新显示数据,然后执行修改事件中的代码,修改数据库的诗句之后,最后一句BindData();重新绑定到页面上。这样应该正确了。我写的代码是在你的代码的基础上简单的修改了一下,不能保证都对。也没有测试,大概思路是这个样子。推荐楼主好好看看页面周期的东西,对初学者来说蛮重要的
      

  2.   


    protected void Page_Load(object sender, EventArgs e)
      {
      if(!IsPostBack)
    {
      Session["newid"] = Request["id"];
      int i = Convert.ToInt32(Session["newid"]);  Conn myconn = new Conn();
      string sql = "SELECT * FROM newInfo WHERE ID =" + i;
      DataSet ds = myconn.myds(sql);
      this.txtTitle.Text = ds.Tables[0].Rows[0][1].ToString();
      this.Content.Text = ds.Tables[0].Rows[0][3].ToString();
      ds.Clear();
    }
      }
      

  3.   

    你的错误在于每次你点击BUTTON按钮时都会POSTBACK一次,重新和服务器建立交互,所以你的值还是原来的值
      

  4.   


       SqlConnection connection = new SqlConnection(connectionString);
       connection.Open();
       try
       {
        SqlDataAdapter adapter = new SqlDataAdapter();
        string sql = "SELECT * FROM newInfo WHERE ID =" + i;
        adapter.SelectCommand = new SqlCommand(sql, connection);
        adapter.SelectCommand.Parameters.Add("@ID", SqlDbType.Int, 4, ID);
        adapter.SelectCommand.Parameters[0].Value = id;
        adapter.UpdateCommand = new SqlCommand("UPDATE jiuye SET jiuyebiaozhiid = @Value WHERE xuehao = @ID", connection);
        string sql2 = "UPDATE newInfo SET title = '" + tt + "',intro = '" + info + "' WHERE ID = " + Convert.ToInt32(Session["newid"]);
        DataSet ds = new DataSet();
        adapter.Fill(ds);
        if(ds.Tables[0].Rows.Count > 0)
        {
         ds.Tables[0].Rows[0]["oldid"] = newValue;
         adapter.Update(ds);
        }
       }
       finally
       {
        connection.Close();
       }
      

  5.   

      SqlConnection connection = new SqlConnection(connectionString);
      connection.Open();
      try
      {
      SqlDataAdapter adapter = new SqlDataAdapter();
      string sql = "SELECT * FROM newInfo WHERE ID =" + i;
      adapter.SelectCommand = new SqlCommand(sql, connection);
      adapter.SelectCommand.Parameters.Add("@ID", SqlDbType.Int, 4, ID);
      adapter.SelectCommand.Parameters[0].Value = id;
      string sql2 = "UPDATE newInfo SET title = '" + tt + "',intro = '" + info + "' WHERE ID = " + Convert.ToInt32(Session["newid"]);
      adapter.UpdateCommand = new SqlCommand(sql2, connection);
      DataSet ds = new DataSet();
      adapter.Fill(ds);
      if(ds.Tables[0].Rows.Count > 0)
      {
      ds.Tables[0].Rows[0]["oldid"] = newValue;
      adapter.Update(ds);
      }
      }
      finally
      {
      connection.Close();
      }