0

我有一个网络表单,它执行以下操作。

  1. 在页面初始化上,我有一个填充的下拉列表。这部分工作正常,并且包含在if(!IsPostBack) {}例程中。这部分工作正常。

  2. 现在,当我从下拉列表中选择一个值时,我的所有文本框字段都填充了数据库中的值,并且字段在编辑时灰显。这发生在 aif(IsPostBack) {}中也很好。

  3. 我有一个复选框,单击该复选框后,所有文本框字段现在都可用于编辑。这也发生在 if (IsPostBack) {} 中,用于重新启用例程中的所有文本框字段

    protected  void performUpdate_CheckedChanged(object sender, EventArgs e)
    {
        if (performUpdate.Checked)
        {
            performDeactivate.Enabled = false;
    
            txtFirstName.Enabled = true;
            txtLastName.Enabled = true;
    
            txtContactNumber.Enabled = true;
         }  // just enabling all of the textbox fields do not worry about closing brackets.
    

现在问题来了,当我更改文本框上的文本并单击提交时——它有一个更新数据库的例程,数据库没有更新。就好像我提交了完全相同的值。除了该UpdateDate字段带有当前时间的时间戳。

这是我的提交按钮代码..

   if (IsPostBack)
       {
   using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["3stepMacro"].ToString()))
           {
               bldSQL = "UPDATE dbo.LoanOwnerStamp SET  FirstName =@FirstName,LastName = @LastName,ContactNumber = @ContactNumber,ContactExt= @ContactExt ,EmailAddress= @EmailAddress,Fax= @Fax, LocationCity= @LocationCity, LocationState = @LocationState,BeginWorkHrs= @BeginWorkHrs,BeginWorkTime = @BeginWorkTime,EndWorkHrs= @EndWorkHrs,EndWorkTime= @EndWorkTime,Zone= @Zone,TeamMemberLanId= @TeamMemberLanId,ManagerId= @ManagerId ,UpdatedDate= @UpdateDate WHERE  TeamMemberLanId = @TeamMemberLanId";
               //   using (SqlCommand updatetLSTeamMember = new SqlCommand("sp_UpdateTeamMemberProfile", myConnection))
               using (SqlCommand updatetLSTeamMember = new SqlCommand(bldSQL, myConnection))
               {
                   //  updatetLSTeamMember.CommandType = CommandType.StoredProcedure;
                   //   string wrokingHrs = ddlBeginHrs.SelectedValue + " " + ddlBeginTime.SelectedValue + " " + lblTo.Text + " " + ddlEndHrs.SelectedValue + " " + ddlEndTime.SelectedValue;
                   /*  updatetLSTeamMember.Parameters.Add("@FirstName", SqlDbType.VarChar, 25).Value = txtFirstName.Text;
                     updatetLSTeamMember.Parameters.Add("@LastName", SqlDbType.VarChar, 30).Value = txtLastName.Text;
                     updatetLSTeamMember.Parameters.Add("@ContactNumber", SqlDbType.VarChar, 20).Value = txtContactNumber.Text;
                     updatetLSTeamMember.Parameters.Add("@ContactExt", SqlDbType.VarChar, 20).Value = txtContactExt.Text;
                     updatetLSTeamMember.Parameters.Add("@EmailAddress", SqlDbType.VarChar, 100).Value = txtEmailAddress.Text;
                     updatetLSTeamMember.Parameters.Add("@Fax", SqlDbType.VarChar, 25).Value = txtFax.Text;
                     updatetLSTeamMember.Parameters.Add("@LocationCity", SqlDbType.VarChar, 20).Value = txtLocationCity.Text;
                     updatetLSTeamMember.Parameters.Add("@LocationState", SqlDbType.VarChar, 20).Value = txtState.Text;
                     updatetLSTeamMember.Parameters.Add("@BeginWorkHrs", SqlDbType.Int).Value = ddlBeginHrs.SelectedValue.ToString();
                     updatetLSTeamMember.Parameters.Add("@BeginWorkTime", SqlDbType.VarChar, 20).Value = ddlBeginTime.SelectedValue.ToString();
                     updatetLSTeamMember.Parameters.Add("@EndWorkHrs", SqlDbType.Int).Value = ddlEndHrs.SelectedValue.ToString();
                     updatetLSTeamMember.Parameters.Add("@EndWorkTime", SqlDbType.VarChar, 20).Value = ddlEndTime.SelectedValue.ToString();
                     updatetLSTeamMember.Parameters.Add("@Zone", SqlDbType.VarChar, 20).Value = ddlZone.SelectedItem.Text;
                     updatetLSTeamMember.Parameters.Add("@TeamMemberLanId", SqlDbType.VarChar, 20).Value = txtTeamMemberLaniId.Text;
                     updatetLSTeamMember.Parameters.Add("@ManagerId", SqlDbType.VarChar, 20).Value = gblUserId;
                     updatetLSTeamMember.Parameters.Add("@UpdateDate", SqlDbType.DateTime).Value = DateTime.Now.ToLocalTime(); */

                   updatetLSTeamMember.Parameters.AddWithValue("@FirstName", gblFirstName );
                   updatetLSTeamMember.Parameters.AddWithValue("@LastName", txtLastName.Text);
                   updatetLSTeamMember.Parameters.AddWithValue("@ContactNumber", txtContactNumber.Text);
                   updatetLSTeamMember.Parameters.AddWithValue("@ContactExt", txtContactExt.Text);
                   updatetLSTeamMember.Parameters.AddWithValue("@EmailAddress", txtEmailAddress.Text);
                   updatetLSTeamMember.Parameters.AddWithValue("@Fax", txtFax.Text);
                   updatetLSTeamMember.Parameters.AddWithValue("@LocationCity", txtLocationCity.Text);
                   updatetLSTeamMember.Parameters.AddWithValue("@LocationState", txtState.Text);
                   updatetLSTeamMember.Parameters.AddWithValue("@BeginWorkHrs", ddlBeginHrs.SelectedValue.ToString());
                   updatetLSTeamMember.Parameters.AddWithValue("@BeginWorkTime", ddlBeginTime.SelectedValue.ToString());
                   updatetLSTeamMember.Parameters.AddWithValue("@EndWorkHrs", ddlEndHrs.SelectedValue.ToString());
                   updatetLSTeamMember.Parameters.AddWithValue("@EndWorkTime", ddlEndTime.SelectedValue.ToString());
                   updatetLSTeamMember.Parameters.AddWithValue("@Zone", ddlZone.SelectedItem.Text);
                   updatetLSTeamMember.Parameters.AddWithValue("@TeamMemberLanId", txtTeamMemberLaniId.Text);
                   updatetLSTeamMember.Parameters.AddWithValue("@ManagerId", gblUserId);
                   updatetLSTeamMember.Parameters.AddWithValue("@UpdateDate", DateTime.Now.ToLocalTime());

                   updatetLSTeamMember.CommandText = bldSQL;
                   updatetLSTeamMember.CommandType = CommandType.Text;

                   // pID = updatetLSTeamMember.Parameters.Add("@return_value", SqlDbType.Int);
                   //   pID.Direction = ParameterDirection.Output;
                   try
                   {
                       if (updatetLSTeamMember.Connection.State == ConnectionState.Closed)
                       {
                           updatetLSTeamMember.Connection.Open();
                       }

                       // string idcat = updatetLSTeamMember.ExecuteNonQuery().ToString();
                       rowsAffected = updatetLSTeamMember.ExecuteNonQuery();

                       //   string idcat =    updatetLSTeamMember.Parameters["@return_value"].SqlValue.ToString();

                       if (rowsAffected == 1)
                       {
                           success = rowsAffected.ToString();
                           lblError.Text = "Successfully Updated a Team Member!";
                           lblCode.Text = success.ToString();
                           return;
                       }

                       else if (rowsAffected == 0)
                       {
                           fail = rowsAffected.ToString();
                           lblError.Text = "Team Member-No Rows Affected !!";
                           lblCode.Text = fail.ToString();
                           return;
                       }

                       /*
                                              rowsAffected = insertLSTeamMember.ExecuteNonQuery();

                                               if (rowsAffected == 1)
                                           {
                                                carryOn = rowsAffected;
                                                lblLoanStamp.Text = "Successfully Added a Manager!";
                                               lblReturnCode.Text = carryOn.ToString();
                                               return;
                                           }

                                               else if (rowsAffected == -1)
                                           {
                                               carryOn = rowsAffected;
                                               lblLoanStamp.Text = "Manager Already exists, Use Update / Deactivate for Maintenance !!";
                                               lblReturnCode.Text = carryOn.ToString();
                                               return;
                                           }  
                       */
                   } // end try

现在我关心的是 Page_load 例程,我必须将它作为if(IsPostBack),因为它必须查询数据库并预填充文本字段。现在我的提交按钮永远不会接受任何文本更改。为什么我的文本框更改的值都没有被捕获并提交到数据库?UpdateDate该表格只是将所有内容按原样发回,并在列上带有新的时间戳。任何帮助表示赞赏。谢谢。

4

1 回答 1

1

您似乎在滥用Page_Load,请按照以下格式进行更新:

protected void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
      //Populate information.
   }
}

protected void chkSomething_Changed(object sender, EventArgs e)
{
   //Do checkbox stuff
}

protected void btnSomething_Clicked(object sender, EventArgs e)
{
   //Update.... You don't need to check postback here. It's a postback, you know this because you caused it.
}
于 2013-08-09T14:50:13.153 回答