简体   繁体   English

将Web窗体控件数据绑定到linq对象的最佳方法是什么?

[英]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值的地方还有更好的选择吗?

ASP.NET MVC would both create these forms and fill these values in on postback for you. ASP.NET MVC会创建这些表单并在回发时为您填写这些值。 Here is an example where both are done. 这是两个都完成的示例

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.

相关问题 在ASP.NET表单上清除控件的最佳方法是什么 - What is the best way to clear controls on an ASP.NET form 在自定义控件中对列表框进行数据绑定的最佳方法是什么 - What is the best way to databind a listbox inside a custom control 将数据提取到Web窗体的ListView中的最佳方法是什么? - What is the best way to pull data into a web form's ListView? 与具有多对多父子关系的表数据绑定ASP.NET TreeView的最佳方法是什么 - What is the Best way to databind an ASP.NET TreeView for table with many to many parent child relationships ASP MVC-动态添加数据控件的最佳方法是什么 - asp mvc - what is the best way to dynamically add data controls 以Web表单显示XML的最佳方法是什么? - Whats the best way to display XML in a web form? 将文件从 angular 11 形式传递到 asp.net 核心 5 web Z8A5DA552ED12654E72Z3 的最佳方式是什么 - What is the best way pass file from angular 11 form to asp.net core 5 web api 在Web应用程序中处理DateTime的最佳方法是什么? - What is the best way to handle DateTime in a Web Application? 在MVC中使用Web API的最佳方法是什么 - what is the best way to consume web api in mvc 推出Web应用程序的最佳方法是什么? - What is the best way to rollout web applications?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM