Automatically saving and restoring a form/ webform

C

catherine

Hi, I was wondering if anyone had any ideas / opinions on what the
best way would be to automatically save to and re-populate a form from
a business object would be.

I usually find myself spending a ton of time hooking up my GUI to my
business objects to save the object to the database and then the same
amount of time in reverse repopulating the GUI from the business
object.

e.g. Say I have a person object

public struct person
{
string firstName;
string lastName;
}

and I want to assign values from a form (where txtFirstName and
txtLastName are textboxes)

person employee = new person();
employee.firstName = txtFirstName.Text;
employee.lastName = txtLastName.Text;

Then repeat the above for multiple forms and then in reverse to re-
populate the form.... boring cookie cutting code..

There must be a better way to do this, has anyone got any better
suggestions/ commercial products/ free products?

Thanks
Catherine
 
M

Marc Gravell

Data-bind the values? i.e.
txtFirstName.DataBindings.Add("Text", employee, "firstName");

(where employee already exists, ideally as a form variable; an source
like List<T>, BindingList<T> or BindingSource would also work in place
in "employee" above)

Now your UI and object model are bound together; the UI will populate
itself from the entity values, and updates to the form controls will
be applied to the object.

For maximum advantage, you should also implement notifications on your
object, commonly via INotifyPropertyChanged (see patter below) - this
means that when you update your object through code (perhaps in a
button-press) the UI can respond to the change(s) and update itself
accordingly.

Marc

public class Employee : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!=null) handler(this, new
PropertyChangedEventArgs(propertyName));
}
private string _name;
public string Name {
get {return _name;}
set {
if(value != _name) {
_name = value;
OnPropertyChanged("Name");
}
}
}

private DateTime _dateOfBirth;
public DateTime DateOfBirth {
get { return _dateOfBirth; }
set {
if (value != _dateOfBirth){
_dateOfBirth = value;
OnPropertyChanged("DateOfBirth");
}
}
}
}
 
M

Marc Gravell

My nntp client (on a small screen) only displayed up to "a form/", so
I missed the "webform" part... the prveious post relates primarily to
WinForm code; WebForm binding works very differently, but does exist.
I'm not really much of an expert in that area, so perhaps search for
"asp.net" and "data binding"...

Marc
 
C

catherine

My nntp client (on a small screen) only displayed up to "a form/", so
I missed the "webform" part... the prveious post relates primarily to
WinForm code; WebForm binding works very differently, but does exist.
I'm not really much of an expert in that area, so perhaps search for
"asp.net" and "data binding"...

Marc


Thanks Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top