-2

当我从 ddl 中选择文本框时,我有填充文本框的代码。这很好用,并且在我的页面加载方法中。

        protected void Page_Load(object sender, EventArgs e)
    {
        //drp_Customer.ClearSelection();


        using (CustomerDataContext obj = new CustomerDataContext())
        {
            // Get fields for drp_Customer
            // ===========================
            var allCustomers = from c in obj.Customers
                               orderby c.FirstName
                               select new
                               {
                                   c.FirstName,
                                   c.CustomerId
                               };

            drp_Customer.DataTextField = "FirstName";
            drp_Customer.DataValueField = "CustomerId";
            drp_Customer.DataSource = allCustomers.ToList();
            drp_Customer.DataBind();

            if (drp_Customer.SelectedItem.Text == " -- Select Customer -- ")
            {
                lbl_message.Text = "Please select a customer to update";
            }
            else
            {
                Customer myCust = obj.Customers.SingleOrDefault(c => c.CustomerId == Convert.ToInt32(drp_Customer.SelectedItem.Value));

                if (myCust != null)
                {
                    txt_FirstName.Text = myCust.FirstName;
                    txt_Surname.Text = myCust.Surname;
                    txt_HouseNumber.Text = myCust.HouseNumberName;
                    txt_Address.Text = myCust.Address;
                    txt_Town.Text = myCust.Town;
                    txt_Telephone.Text = myCust.Telephone;
                    txt_Postcode.Text = myCust.Postcode;
                }

            }

        }


    }

我在页面上有一个更新按钮,该按钮用于更新但由于此代码有效而停止工作。

        protected void btn_Update_Click(object sender, EventArgs e)
    {
        using (CustomerDataContext obj = new CustomerDataContext())
        {
            Customer myCust = obj.Customers.SingleOrDefault(c => c.CustomerId == Convert.ToInt32(drp_Customer.SelectedValue));

            // Check If any are empty 
            // ======================
            if (myCust != null)
            {
                myCust.FirstName = txt_FirstName.Text;
                myCust.Surname = txt_Surname.Text;
                myCust.HouseNumberName = txt_HouseNumber.Text;
                myCust.Address = txt_Address.Text;
                myCust.Town = txt_Town.Text;
                myCust.Telephone = txt_Telephone.Text;
                myCust.Postcode = txt_Postcode.Text;
            }

            obj.SubmitChanges();
        }

    }

我正在查询同一个 customerId,当我单步执行代码时, // myCust.FirstName = txt_FirstName.Text; 当我改变它时不会改变。

页面是否需要刷新才能实施更改?还是我需要以某种方式在页面加载中使用它?

4

1 回答 1

0

看来您正在重新加载所有控件Page_Load,请尝试添加方法!Page.IsPostbackPage_Load像这样的东西:

protected void Page_Load(object sender, EventArgs e)
{
     if (!Page.IsPostback)
     {
         //Your code goes here.
     }
}
于 2013-01-16T13:00:10.683 回答