问题
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.
- Paging - should be set on the GridView level, specifying whether it's allowed or not and how many rows to display (
PageSize
). - CRUD methods -
SelectMethod
,InsertMethod
,DeleteMethod
andUpdateMethod
should be specified on theObjectDataSource
.If you change this toSqlDataSource
you would need to change it on theSqlDataSource
level, but then the property names are slightly different e.gSelectCommand
,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