I have a requirement for pre-setting a field to the last entered value. I'm saving the value on Save()
to a static and then I would like to set the field to that value the next time a new entry form is created.
I cannot get the last step ... I get entity开发者_JS百科 errors saying the entity is already attached.
How can I set the value of a field?
You should be setting the default value in the Created event on the data table. Capture the last value in the Changed event - this will capture every change that the current user makes to the field. Here's an example using an entity called "Widget" that sets the default price to the last price entered by the user:
public partial class Widget
{
private static decimal LastWidgetPrice;
partial void Widget_Created()
{
Price = LastWidgetPrice;
}
partial void Price_Changed()
{
LastWidgetPrice = Price;
}
}
Note that you can't capture the value in the Inserting or Updating events, because these events happen on the server, and the Created event happens on the client. Since the Changed event happens in client code, we can use that event to capture the last value entered.
The Created and Changed events can be created using the WriteCode drop-down on the entity designer (i.e. the table designer).
精彩评论