ASP.NET GridView Model-binding

回眸只為那壹抹淺笑 提交于 2019-12-11 01:04:46

问题


I am new to ASP.NET and I want to write a simple WebApplication using WebForms that connects to a Database and displays some data in a Grid (with paging). I use Visual Studio 2015

For displaying the data I use a GridView in combination with an ObjectDataSource.

I use EntityFramework and a method GetCustomer() that returns all Customers from a database.I use that method as SelectMethod

My question is:
Both controls, GridView an ObjectDataSource, have parameters for Paging and SelectMethod

Which control’s parameter for Paging and SelectMethod should I use if I want to use Model Binding?

Currently I’m using this:

GridView :<br>

AllowPaging     = true<br>
PageSize    = 10<br>
SelectMethod    = ""<br>
DataSourceID    = dsCustomers<br>


ObjectDataSource:<br>
ID                  = dsCustomers<br>
EnablePaging        = false<br>
SelectMethod        = GetCustomers ()<br>
MaximumRowParameterName = ""<br>
StartRowIndexParameterName  = ""<br>


These settings work, the data is displyed and paging is working.
But I am not sure if this is the correct way to do it.

And if I change the settings for Paging and SelectMethod like so

GridView: <br>
AllowPaging     = false<br>
PageSize        = ""<br>
SelectMethod    = GetCustomers()<br>
DataSourceID    = dsCustomers<br>


ObjectDataSource:<br>
ID                  = dsCustomers<br>
EnablePaging                = true<br>
SelectMethod                = <br>
MaximumRowParameterName     = ""<br>
StartRowIndexParameterName  = ""<br>

I get an error :

DataSource or DataSourceID cannot be defined on 'gridViewCustomers' when it uses model binding.

If I remove DataSourceID from the GridView then I get

"A public method with the name 'GetCustomers' was either not found or there were multiple methods with the same name"


回答1:


The way you're doing it in the first example is correct.

  1. Paging - should be set on the GridView level, specifying whether it's allowed or not and how many rows to display (PageSize).
  2. CRUD methods - SelectMethod, InsertMethod , DeleteMethod and UpdateMethod should be specified on the ObjectDataSource.If you change this to SqlDataSource you would need to change it on the SqlDataSource level, but then the property names are slightly different e.g SelectCommand, InsertCommand e.t.c

Note: the EnablePaging property of the ObjectDataSource could most definitely be configured to enable paging on the GridView but it's more complicated and in all the years I've worked with the GridView control I've never seen anyone do it that way. Nevertheless, if you're curious how it can be done have a look at this MSDN article.



来源:https://stackoverflow.com/questions/37695988/asp-net-gridview-model-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!