开发者

problem with linq to sql update using asp.net textboxes

开发者 https://www.devze.com 2023-01-30 08:31 出处:网络
I have a textbox and a button. On page load I select one column from one row and put its value in the textbox. I have a button click method that updates the same column/row with the value in the textb

I have a textbox and a button. On page load I select one column from one row and put its value in the textbox. I have a button click method that updates the same column/row with the value in the textbox.

The problem i'm having is that when I开发者_高级运维 clear the text in the text box, type in new data and hit submit the new text value is not being saved, it uses the old one.

I put a breakpoint at the end of my button click method and it appears that asp.net is sending the old value of the textbox rather than the new one I put in. I'm totally stumped.

  • This problem persists even if I have viewstate false on the textbox.
  • Both of my LINQ queries are wrapped in a using statement.

Any Ideas?

Edit: Here is the full code:

 protected void Page_Load(object sender, EventArgs e)
    {

        using (StoreDataContext da = new StoreDataContext())
        {
            var detail = from a in da.Brands
                         where a.BrandID == 5
                         select new
                         {
                             brand = a,
                         };
            foreach (var d in detail)
            {
                txtEditDescription.Text = d.brand.Description;
            }
        }  

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        using (StoreDataContext dk = new StoreDataContext())
        {

            Brand n = dk.Brands.Single(p => p.BrandID == 5);

        n.Description = txtEditDescription.Text;

        dk.SubmitChanges();
        }
    }


As you said, in Page_Load you are retrieving the original value into the text box which overwrites any changes.

Yes, Page_Load DOES execute before Button Click (or any other control events) is executed. I'm going to guess you have done Windows Forms development before this, the ASP.Net web forms event model is quite different. Check out the ASP.NET Page Life Cycle Overview.


I figured it out. I should be be using if(!IsPostBack) on the code that originally fills the textbox with its value.

However this makes no sense to me. If the page is loaded and the textbox text gets a value, then you clear this value and try to insert it into the database, it should insert this value and then when the page post back it will fetch the new value from the database and put the value in the textbox.

The way it's actually working makes it seem like it is executing the page load code before the button click code is executed on post back.


just to trace the error,please try to put a label =( lblDescription.Text ) and leave the rest of code as is,put the new value in the textbox (editdescription.text) try it and tell me what you see

 foreach (var d in detail) 
        { 
           lblDescription.Text = d.brand.Description; 
        } 
0

精彩评论

暂无评论...
验证码 换一张
取 消