加载和更新表单数据时出错

时间:2012-05-17 20:14:25

标签: c# asp.net

我在ASP.NET中有一个表单,如果用户存在数据,则加载数据,然后允许他们更新。所以在Page_Load我有:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckAndAddRecord(_currentUser.UserID))
        {
            CreateEntryPoint(_currentUser.UserID);
        } 
        else
        {
            DataView dv = LoadApplicationData(_currentUser.UserID);
            foreach (DataRowView rowView in dv)
            {
                DataRow row = rowView.Row;

                _applicationId = row["id"].ToString();

                txtProjectNumber.Text = row["ProjectNumber"].ToString();
                  if (String.IsNullOrEmpty(chkSignedNDANo.ToString()) ||  String.IsNullOrEmpty(chkSignedNDAYes.ToString()))
                  {
                     chkSignedNDANo.Checked = Convert.ToBoolean(row["SignedNDAOnFile"]);
                     chkSignedNDAYes.Checked = Convert.ToBoolean(row["SignedNDAOnFile"]);
                  }
                  txtDateWritten.Text = row["DateWritten"].ToString();
            }
        }
    }

它称之为:

    public bool CheckAndAddRecord(int UserId)
    {
        string connStr = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("CheckUserInTemp", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@SubmittedBy", UserId));
        SqlDataAdapter dap = new System.Data.SqlClient.SqlDataAdapter(cmd);
        DataSet ds = new DataSet();

        // open conn
        if (conn.State == ConnectionState.Closed)
            conn.Open();

        // fill
        dap.Fill(ds);

        // close the conn
        if (conn.State == ConnectionState.Open)
            conn.Close();

        bool boolRecordExist = false;

        if (ds.Tables[0].DefaultView.Count == 0)
        {
            boolRecordExist = false;
        } 
        else
        {
            boolRecordExist = true;
        }

        return boolRecordExist;
    }

这似乎可以正常工作并填充字段。但随后更新我调用了一个Button click,它调用了这个方法:

    public void UpdateRecordInTemp(int appId, string projectNumber)
    {
        string connStr = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand("CPC_ProposalUpdateTemp", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@id", appId);
        cmd.Parameters.AddWithValue("@ProjectNumber", txtProjectNumber.Text);

        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw new Exception("Exception adding coupon. " + ex.Message);
        }
        finally
        {
            conn.Close();
        }
    }

问题是传递的字段总是包含旧值。因此,如果输入页面时的值为“123”并将其更改为“abc”并单击按钮,则会传递“123”而不是“abc”。

很抱歉这篇长篇文章,我想了解所有细节。我对此感到困惑。谢谢!

1 个答案:

答案 0 :(得分:0)

IsPostBack的页面加载检查if属性。似乎你的代码在每个页面加载时都在执行。

if(!IsPostBack)
{
       // Do your work
}

更新

在您的代码方法UpdateRecordInTemp(int appId, string projectNumber)中,您没有使用projectNumber而是使用txtProjectNumber.Text