我们如何使用强类型的模型绑定与GridView
的BoundField
? BoundField
的DataField
属性似乎只取一个字符串值,不像FormView
,我可以使用适当的模型绑定语法,如Prop="<%# MyFieldName %>"
。 TemplateField
是我唯一的选择吗?GridView和Model Binding
0
A
回答
1
对于任何未来的读者,TemplateField
是你唯一的选择,如果你想利用强类型的绑定和智能等
0
我不确定你想要什么,但我仍然创建了一个这样的小应用程序。
这里是aspx页面中的gridview。
<asp:GridView runat="server" ID="grdEmployee" AutoGenerateColumns="False" ModelType="WebApplication1.Employee">
<Columns>
<asp:BoundField DataField="Age" HeaderText="Age"/>
<asp:BoundField DataField="Name" HeaderText="Name"/>
</Columns>
</asp:GridView>
现在我创建了如下所示的Employee类。
public class Employee
{
public string Name { get; set; }
public int Age { get; set; }
}
和在asp.net的page_load事件我写这个。
protected void Page_Load(object sender, EventArgs e)
{
List<Employee> employees=new List<Employee>();
Employee employee=new Employee{Age = 33,Name = "Jalpesh Vadgama"};
employees.Add(employee);
employee = new Employee { Age = 30, Name = "Vishal Vadgama" };
employees.Add(employee);
grdEmployee.DataSource = employees;
grdEmployee.DataBind();
}
它的工作原理。对于双向绑定,您需要使用bindItem的模板控件。
参考 - http://www.sitepoint.com/asp-net-4-5-strongly-typed-data-controls-model-binding/
相关问题
- 1. CA2227和ASP.NET Model Binding
- 2. Checkboxfor not binding model
- 3. MVVM - ViewModel/Model binding
- 4. model binding html:asp not working
- 5. Angular programatically binding ng-model
- 6. Model binding addonLeft.text angular formly
- 7. ASP.NET Core 2 MVC App“POCO”Model binding not binding
- 8. AngularJS KendoUI DatePicker Model not binding
- 9. binding ng-model to ionic popover
- 10. System.StackOverflowException未处理(MVC Model BINDING)
- 11. MVC Model not binding to HTTPPost action
- 12. Form Encoded Javascript int [] not Binding Model Binder
- 13. AngularJS ng-model binding input type = email元素
- 14. DateTime和ASP.NET MVC 3 Model Binding的全球化问题
- 15. WPF,TabControl和Binding
- 16. WPF ControlTemplate和Binding
- 17. UWP ProgressBar和Binding
- 18. {binding}和{binding Account}有什么区别?
- 19. C++和xml binding simpletype
- 20. WPF,Datatemplates和Data binding
- 21. Dictionary <short,Dictionary <EnFunction,bool >> model binding not working
- 22. MVVM treeview wpf(binding?)
- 23. resize event not binding on binding function
- 24. 请参阅GridView中的Yii2 ActiveRecord/model列?
- 25. Jgoodies JTable Binding,Swing
- 26. Binding CustomDateEditor spring Mvc
- 27. MVC 2,IoC,ModelView和Binding
- 28. ToggleButton binding
- 29. Knockout Compose Binding
- 30. MVVM Light Binding
您提供不模型绑定的代码。这是纯粹的做事方式。然而,您发现的链接是正确的模型绑定;不知道你是否有机会通过它。但确切的问题是,代码中的'DataField'属性需要像'Age'和'Name'这样的字符串值,它没有类似于intellisense等强类型模型绑定的好处。但是TemplateField支持模型绑定如文章中所示。我试图用'BoundField'做同样的事情。 – dotNET
你不能绑定boundfield –