[英]What is the best way to databind Web Form controls to linq objects?
I have a typical object (it comes from Linq but it could have been create manually): 我有一个典型的对象(它来自Linq,但可以手动创建):
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: 我有一个Web表单,用户可以在其中输入以下数据:
<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? 但是在类或Linq上自动吸收Control值的地方还有更好的选择吗?
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.