页面上放一个TEXTBOX  txtCollege,一个BUTTON,根据前面页传来ID,查询出一条记录,txtCollege中为查询出的某一字段的值,string sql="SELECT * FROM College WHERE ID="+request["id"].tostring();
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["NoteConnectionString"].ToString()))
        {
            cn.Open();
            using (SqlCommand cmd = new SqlCommand(sql, cn))
            {
                try
                {
                    cmd.ExecuteReader();
                    txtCollege.text=dr["College"].tostring();
                }
                catch (SqlException ex)
                {
                    cn.Close();
                    throw new Exception(ex.Message);
                }
                if (cmd != null)
                    cmd.Dispose();
            }
        }
修改txtCollege可的信息,点击BUTTON进行修改,修改后,为什么College的值并没有变化,跟踪了一下发现txtCollege.Text是修改前的,我明明修改了,为什么提交到数据库里的值是修改前的呢?直接这样写的
        
int reg=0;
string sql = "UPDATE College SET College='" + txtCollege.Text + "' WHERE ID=" + Request["ID"].ToString();
        using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["NoteConnectionString"].ToString()))
        {
            cn.Open();
            using (SqlCommand cmd = new SqlCommand(sql, cn))
            {
                try
                {
                    ret=cmd.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    cn.Close();
                    throw new Exception(ex.Message);
                }
                if (cmd != null)
                    cmd.Dispose();
            }
        }
if (ret!=0)
response.write("<script language=javascript>alert('成功');</script>");
else
response.write("<script language=javascript>alert('失败');</script>");
然后提示修改成功

解决方案 »

  1.   

    if(!IspostBack)
    {
    //绑定数据
    }
      

  2.   

    恩,就是 IsPostBack 的问题
      

  3.   

    protected void Page_Load(object sender, EventArgs e)
        {
    if (!Page.IsPostBack)
            {string sql="SELECT * FROM College WHERE ID="+Request["id"].tostring();
    using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["NoteConnectionString"].ToString()))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(sql, cn))
                {
                    try
                    {
                        cmd.ExecuteReader();
                        txtCollege.text=dr["College"].tostring();
                    }
                    catch (SqlException ex)
                    {
                        cn.Close();
                        throw new Exception(ex.Message);
                    }
                    if (cmd != null)
                        cmd.Dispose();
                }
            }        }
    }