0

以下代码来自详细信息页面,该页面从另一个页面接收查询字符串,然后根据传递的 id 加载详细信息。这很好,但现在我的问题是保存更改按钮似乎没有做它应该做的事情;页面就像它提交一样闪烁并加载所有值,我已经调试过,一切都很好,可能是什么问题?它是否与我使用查询字符串有关,因为我在另一个页面上有另一个更新命令(没有查询字符串)并且它工作得很好......

    namespace CC
{
    public partial class customerdetails : System.Web.UI.Page
    {
        int customerID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            customerID = Convert.ToInt32(Request.QueryString["id"]);
            string strConnString2 = "Data Source=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            using (SqlConnection con = new SqlConnection(strConnString2))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SELECT CustomerStatus, CustomerGroup, CustomerFirstName, CustomerLastName, CompanyName, CustomerAddress, CustomerCity, CustomerState, CustomerZipcode, CustomerEmail, CustomerEmail2, CustomerPhoneNumber, CustomerPhoneNumberExt, CustomerType, CustomerSubscriptionType, CustomerCost, CustomerPaymentMethod, CustomerSignUpDate, CustomerPickUpDay, CustomerPickUpDay2, CustomerNotes FROM Customers WHERE CustomerID = @CustomerID";
                    cmd.Parameters.AddWithValue("@CustomerID", customerID);
                    con.Open();
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            if (reader["CustomerStatus"].ToString() == "New")
                            {
                                StatusLabel.ForeColor = System.Drawing.Color.YellowGreen;
                            }
                            else
                            {
                                if (reader["CustomerStatus"].ToString() == "Suspended")
                                {
                                    StatusLabel.ForeColor = System.Drawing.Color.Aqua;
                                }
                                else
                                {
                                    StatusLabel.ForeColor = System.Drawing.Color.DarkRed;
                                }
                            }
                            StatusLabel.Text = reader["CustomerStatus"].ToString();
                            if (reader["CustomerGroup"].ToString() == "")
                            {
                            }
                            else
                            {
                                GroupDropDown.SelectedValue = reader["CustomerGroup"].ToString();
                            }
                            txtCustomerFirstName.Text = reader["CustomerFirstName"].ToString();
                            txtCustomerLastName.Text = reader["CustomerLastName"].ToString();
                            txtCompanyName.Text = reader["CompanyName"].ToString();
                            txtCustomerAddress.Text = reader["CustomerAddress"].ToString();
                            txtCustomerCity.Text = reader["CustomerCity"].ToString();
                            txtCustomerState.Text = reader["CustomerState"].ToString();
                            txtCustomerZipcode.Text = reader["CustomerZipcode"].ToString();
                            txtCustomerEmail.Text = reader["CustomerEmail"].ToString();
                            if (reader["CustomerEmail2"].ToString() == "")
                            {

                            }
                            else
                            {
                                Email2CheckBox.Checked = true;
                                txtCustomerEmail2.Visible = true;
                                txtCustomerEmail2.Text = reader["CustomerEmail2"].ToString();
                            }
                            txtCustomerPhoneNumber.Text = reader["CustomerPhoneNumber"].ToString();
                            txtCustomerPhoneNumberExt.Text = reader["CustomerPhoneNumberExt"].ToString();
                            CustomerType.SelectedValue = reader["CustomerType"].ToString();
                            SubscriptionType.SelectedValue = reader["CustomerSubscriptionType"].ToString();
                            CostTxtBx.Text = reader["CustomerCost"].ToString();
                            CustomerPaymentMethod.SelectedValue = reader["CustomerPaymentMethod"].ToString();
                            CustomerSignUpDate.Text = reader["CustomerSignUpDate"].ToString();
                            PickUpDay.SelectedValue = reader["CustomerPickUpDay"].ToString();
                            if (reader["CustomerPickUpDay2"].ToString() == "")
                            {

                            }
                            else
                            {
                                PickUpDay2CheckBox.Checked = true;
                                PickUpDay2.Visible = true;
                                PickUpDay2.SelectedValue = reader["CustomerPickUpDay2"].ToString();
                            }
                            if (reader["CustomerNotes"].ToString() == "")
                            {
                            }
                            else
                            {
                                string sqlfriendlyNotes = reader["CustomerNotes"].ToString().Replace("<br/>", "\n");

                                NotesTxtBx.Text = sqlfriendlyNotes;
                            }

                        }
                    }
                    con.Close();
                }
            }
        }

        protected void SaveCustomerChanges_Click(object sender, EventArgs e)
        {
            string Status = this.StatusLabel.Text;
            string Group = this.GroupDropDown.Text;
            string customerStatus = Status;
            string customerFirstName = this.txtCustomerFirstName.Text;
            string customerLastName = this.txtCustomerLastName.Text;
            string customerGroup = Group;
            string companyName = this.txtCompanyName.Text;
            string customerAddress = this.txtCustomerAddress.Text;
            string customerCity = this.txtCustomerCity.Text;
            string customerState = this.txtCustomerState.Text;
            string customerZipcode = this.txtCustomerZipcode.Text;
            string customerEmail = this.txtCustomerEmail.Text;
            string customerEmail2 = this.txtCustomerEmail2.Text;
            string customerPhoneNumber = this.txtCustomerPhoneNumber.Text;
            string customerPhoneNumberExt = this.txtCustomerPhoneNumberExt.Text;
            string customerType = this.CustomerType.Text;
            string customerSubscriptionType = this.SubscriptionType.Text;
            string customerCost = this.CostTxtBx.Text;
            string customerPaymentMethod = this.CustomerPaymentMethod.Text;
            string customerSignUpDate = this.CustomerSignUpDate.Text;
            string customerPickUpDay = this.PickUpDay.Text;
            string customerPickUpDay2 = this.PickUpDay2.Text;
            string customerNotes = this.NotesTxtBx.Text.Replace("\n", "<br/>");
            string strConnString1 = "Data Source=xxxxxxxxxxxxxx";
            using (SqlConnection con = new SqlConnection(strConnString1))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "UPDATE Customers SET CustomerStatus=@CustomerStatus, CustomerGroup=@CustomerGroup, CustomerFirstName=@CustomerFirstName, CustomerLastName=@CustomerLastName, CompanyName=@CompanyName, CustomerAddress=@CustomerAddress, CustomerCity=@CustomerCity, CustomerState=@CustomerState, CustomerZipcode=@CustomerZipcode, CustomerEmail=@CustomerEmail, CustomerEmail2=@CustomerEmail2, CustomerPhoneNumber=@CustomerPhoneNumber, CustomerPhoneNumberExt=@CustomerPhoneNumberExt, CustomerType=@CustomerType, CustomerSubscriptionType=@CustomerSubscriptionType, CustomerCost=@CustomerCost, CustomerPaymentMethod=@CustomerPaymentMethod, CustomerSignUpDate=@CustomerSignUpDate, CustomerPickUpDay=@CustomerPickUpDay, CustomerPickUpDay2=@CustomerPickUpDay2, CustomerNotes=@CustomerNotes WHERE CustomerID = @CustomerID";
                    cmd.Parameters.AddWithValue("@CustomerID", customerID);
                    cmd.Parameters.AddWithValue("@CustomerStatus", customerStatus);
                    cmd.Parameters.AddWithValue("@CustomerGroup", customerGroup);
                    cmd.Parameters.AddWithValue("@CustomerFirstName", customerFirstName);
                    cmd.Parameters.AddWithValue("@CustomerLastName", customerLastName);
                    cmd.Parameters.AddWithValue("@CompanyName", companyName);
                    cmd.Parameters.AddWithValue("@CustomerAddress", customerAddress);
                    cmd.Parameters.AddWithValue("@CustomerCity", customerCity);
                    cmd.Parameters.AddWithValue("@CustomerState", customerState);
                    cmd.Parameters.AddWithValue("@CustomerZipcode", customerZipcode);
                    cmd.Parameters.AddWithValue("@CustomerEmail", customerEmail);
                    cmd.Parameters.AddWithValue("@CustomerEmail2", customerEmail2);
                    cmd.Parameters.AddWithValue("@CustomerPhoneNumber", customerPhoneNumber);
                    cmd.Parameters.AddWithValue("@CustomerPhoneNumberExt", customerPhoneNumberExt);
                    cmd.Parameters.AddWithValue("@CustomerType", customerType);
                    cmd.Parameters.AddWithValue("@CustomerSubscriptionType", customerSubscriptionType);
                    cmd.Parameters.AddWithValue("@CustomerCost", customerCost);
                    cmd.Parameters.AddWithValue("@CustomerPaymentMethod", customerPaymentMethod);
                    cmd.Parameters.AddWithValue("@CustomerSignUpDate", customerSignUpDate);
                    cmd.Parameters.AddWithValue("@CustomerPickUpDay", customerPickUpDay);
                    cmd.Parameters.AddWithValue("@CustomerPickUpDay2", customerPickUpDay2);
                    cmd.Parameters.AddWithValue("@CustomerNotes", customerNotes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
4

1 回答 1

2

您的按钮单击事件处理程序SaveCustomerChanges_Click将在您的Page_Load事件处理程序之后发生。您的页面加载事件处理程序正在加载您的 UI。在您的 UI 已经加载之后,您正在更新数据。您需要在执行更新后重新加载 UI(想想“DataBind”)。

有关 ASP.NET 页面生命周期的概述,请参阅以下 msdn 文章:http: //msdn.microsoft.com/en-us/library/ms178472.aspx

于 2012-06-29T17:09:03.883 回答