我前台页放了几个text,可是点击提交后,后台总是提交text里原有的字符,我修改的就返回不了,为什么啊,高手帮忙!!!!!
代码如下:
   protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = db.createcon();
            con.Open();
            SqlCommand com = new SqlCommand("select webname,website,webtel,webcopyright from fw_config", con);
            SqlDataReader dr= com.ExecuteReader();
            dr.Read();
            this.txtwebname.Text = dr.GetValue(0).ToString();
            this.txtwebsite.Text = dr.GetValue(1).ToString();
            this.txtwebtel.Text = dr.GetValue(2).ToString();
            this.txtwebcopyright.Text = dr.GetValue(3).ToString();
            dr.Close();
            com.Dispose();
            con.Close();
        }
        catch (Exception)
        {
            Response.Write("<script>alert('提取数据错误!');</script>");
            Response.End();
        }
    }
    protected void setsave_Click(object sender, EventArgs e)
    {
        string webname = txtwebname.Text;
        string website = txtwebsite.Text;
        string webtel = txtwebtel.Text;
        string webcopyright = txtwebcopyright.Text;        Response.Write("<script>alert('" + webname + "');</script>");
        try
        {
            SqlConnection con = db.createcon();
            con.Open();
            SqlCommand com = new SqlCommand("update fw_config set webname='"+webname+"',website='"+website+"',webtel='"+webtel+"',webcopyright='"+webcopyright+"'", con);
            com.ExecuteNonQuery();
            com.Dispose();
            con.Close();
            Response.Write("<script>alert('修改完成!');location='config.aspx';</script>");
        }
        catch (Exception)
        {
            Response.Write("<script>alert('异常错误,更新失败!');</script>");
            Response.End();
        }    }

解决方案 »

  1.   

    if(!Ispostback)    //加上這句~~~~~
    {
       try
            {
                SqlConnection con = db.createcon();
                con.Open();
                SqlCommand com = new SqlCommand("select webname,website,webtel,webcopyright from fw_config", con);
                SqlDataReader dr= com.ExecuteReader();
                dr.Read();
                this.txtwebname.Text = dr.GetValue(0).ToString();
                this.txtwebsite.Text = dr.GetValue(1).ToString();
                this.txtwebtel.Text = dr.GetValue(2).ToString();
                this.txtwebcopyright.Text = dr.GetValue(3).ToString();
                dr.Close();
                com.Dispose();
                con.Close();
            }
            catch (Exception)
            {
                Response.Write("<script>alert('提取数据错误!');</script>");
                Response.End();
            }}
      

  2.   

    click 提交按鈕,頁面首先實行的是load事件~~~~
    加上!ispostback 可以防止對頁面重新初始化~~~---
    lz可以單步調試一下~~看看執行順序~
      

  3.   

    if(!Ispostback)
    {}
    少了这个