ASP.NET GridView模型绑定 [英] ASP.NET GridView Model-binding

查看:125
本文介绍了ASP.NET GridView模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ASP.NET的新手,我想使用连接到数据库并在Grid(带有分页)的Grid中显示一些数据的WebForm编写一个简单的WebApplication.我使用Visual Studio 2015

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

为了显示数据,我将GridView与ObjectDataSource结合使用.

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

我使用EntityFramework和方法GetCustomer()从数据库返回所有客户.我将该方法用作SelectMethod

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

我的问题是:
这两个控件GridView和ObjectDataSource都具有Paging and SelectMethod

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

如果要使用Model Binding,应该使用哪个控件的Paging和SelectMethod参数?

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

当前我正在使用此

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>


这些设置有效,显示数据并且分页有效.
但是我不确定这是否是正确的方法.

如果我像这样更改Paging and SelectMethod的设置

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>

我得到一个错误:

DataSource或DataSourceID不能在'gridViewCustomers'上定义 使用模型绑定时.

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

如果我从GridView中删除DataSourceID,则会得到

If I remove DataSourceID from the GridView then I get

"找不到名为"GetCustomers"的公用方法 有多个同名方法"

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

推荐答案

第一个示例中的处理方法是正确的.

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

  1. 分页-应该在GridView级别上设置,指定是否允许分页以及要显示的行数(PageSize).
  2. CRUD方法-应在ObjectDataSource上指定SelectMethodInsertMethodDeleteMethodUpdateMethod.如果将其更改为SqlDataSource,则需要在SqlDataSource级别上进行更改,但属性名称略有不同,例如SelectCommandInsertCommand
  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

注意:ObjectDataSourceEnablePaging属性可以绝对地配置为在GridView上启用分页,但是它更加复杂,并且在我使用GridView控件的所有年份中从来没有见过有人那样做.不过,如果您好奇如何完成此操作,请查看

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.

这篇关于ASP.NET GridView模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆