0

I have a pretty odd problem. I was working on an item editing page end encountered some odd bug, that ASP passess old values to C# .cs code.

In my Page_Load

private int SomeID = 0;
if (!IsPostBack) {
    ...
    SomeID = Convert.ToInt32(Page.RouteData.Values["id"])
    LoadFunction();
}

Loading function:

DataBaseDataContext db = new DataBaseDataContext();
var Item = db.FirstOrDefault(k => k.ID == SomeID);

NameTextBox.Text = Item.Name;
PriceTextBox.Text = Item.Price.ToString();

Saving function:

DataBaseDataContext db = new DataBaseDataContext();
var Item = db.FirstOrDefault(k => k.ID == SomeID);

Item.Name = NameTextBox.Text;
Item.Price = Convert.ToDecimal(PriceTextBox.Text);
...
db.SubmitChanges();

I was bothering, why it doesn't save changes for me, so I set breakpoint in db.SubmitChanges() in the saving function (loading works fine). But when I looked up in value preview in VisualStudio, it showed me that it wants to send previously loaded values and not those I edited in my form.

I must be missing something, as it works in other places of my code, but I have no idea what it is.

4

3 回答 3

2

在 !IsPostback 之外设置 SomeID

于 2013-05-17T09:29:54.410 回答
1

尝试在您的活动中使用Page.Ispostback属性,例如Page_Load

//Page_Load Event
if (!Page.IsPostBack)
    {
         DataBaseDataContext db = new DataBaseDataContext();
         var Item = db.FirstOrDefault(k => k.ID == SomeID);

         NameTextBox.Text = Item.Name;
         PriceTextBox.Text = Item.Price.ToString();  
    }

查看MSDN

if 将保留您的旧主要价值并解决您的问题,而不是privateprivate int SomeID = 0使用中。static

希望对你有效。

于 2013-05-17T09:10:46.817 回答
1

检查您是否没有再次PostBack为这些esPage_Load设置值。TextBox

if (!Page.IsPostBack)
{
   DataBaseDataContext db = new DataBaseDataContext();
   var Item = db.FirstOrDefault(k => k.ID == SomeID);

   NameTextBox.Text = Item.Name;
   PriceTextBox.Text = Item.Price.ToString();  
}
于 2013-05-17T09:11:20.380 回答