views:

147

answers:

2

I have a typical object (it comes from Linq but it could have been create manually):

public class Person
{
  public string FirstName{get; set;}
  public string LastName{get; set;}
  public int Age{get; set;}
}

And I have a Web Form where users enter this data:

<asp:TextBox runat="server" ID="FirstName"></asp:TextBox>
<asp:TextBox runat="server" ID="LastName"></asp:TextBox>
<asp:TextBox runat="server" ID="Age"></asp:TextBox>

How do I bind the data from the Control to the class? I could do this:

Person person;
person.FirstName = FirstName.Text;

but is there something better where the class or Linq just sucks in the Control values automatically?

+1  A: 

ASP.NET MVC would both create these forms and fill these values in on postback for you. Here is an example where both are done.

Yuriy Faktorovich
I am aware of that, I was wondering if there was something equivalent in Web Forms?
Petras
@Petras well ASP.NET is open source, so you could try using the code of the DefaultBinder. Or implement it yourself, it should be pretty trivial for simple models.
Yuriy Faktorovich
+1  A: 
DataClassesDataContext db = new DataClassesDataContext();

//Some Event

protected void btn_Submit_Click(object sender, System.EventArgs e)
{

   Person NewP = new Person();
   NewP.FirstName = FirstName.Text;
   NewP.LastName = LastName.Text;
   ...

   db.Persons.InsertOnSubmit(NewP);

   try 
   {
      db.SubmitChanges();
   }
   catch (Exception ex) 
   {
      Response.Write(ex.ToString);
   }
}
H Sampat
I was hoping there was some mechanism to automate this line NewP.FirstName = FirstName.Text;
Petras