Spread for ASP.NET : ASP.NET 4.5 模式绑定

Spread for ASP.NET 表格控件提供了 ASP.NET 4.5 下的模式绑定。Spread 控件提供 ItemType 属性,例如 SelectMethod,UpdateMethod,InsertMethod 和 DeleteMethod 属性用户模式绑定操作。注意,模式绑定功能只适用于 .NET 4.5 及以上版本;在较低版本中使用模式绑定无效。如果使用同时使用模式绑定和 DataSourceID,控件将抛出异常。

发布于 2013/08/02 00:00

SpreadJS

Spread for ASP.NET 表格控件提供了 ASP.NET 4.5 下的模式绑定。Spread 控件提供 ItemType 属性,例如 SelectMethod,UpdateMethod,InsertMethod 和 DeleteMethod 属性用户模式绑定操作。

注意,模式绑定功能只适用于 .NET 4.5 及以上版本;在较低版本中使用模式绑定无效。如果使用同时使用模式绑定和 DataSourceID,控件将抛出异常。

ItemType 属性用于声明使用模式绑定中的数据。默认情况下,它是空的。如果你设置 ItemType 和表单的 SelectMethod 属性,.NET Framework 会尝试转换数据为 ItemType 类型。因此,您需要设置 ItemType 和 SelectMethod 属性为同一数据类型。或者设置 SelectMethod 属性为一个父数据类型。否则,控件会抛出异常。

SelectMethod, UpdateMethod, InsertMethod, 和 DeleteMethod 属性用于设置获取、更新、插入和删除数据项对应的方法。注意以下几点:

如果您设置了其中几个属性,它们将自动合并到模式绑定的属性中。

如果您只设置了 SelectMethod 属性,而没有设置其他属性。当对数据进行增删改查时,控件将抛出异常。

属性对应的方法必须是在本页面后台方法中,否则 ,.NET Framework 将抛出异常。当前 >NET Framework 仅支持公共方法。

使用代码:

创建数据源。

绑定 Spread 表格控件到数据源。

如果你愿意,可以设置 ItemType 属性。

设置 SelectMethod, UpdateMethod, InsertMethod, 和 DeleteMethod 属性为对应的后台方法。

示例

该实例演示了如何实现模式绑定。

  1: <Sheets>
  2: <FarPoint:SheetView SheetName="Sheet1"
  3: AllowDelete="true" AllowInsert="true"
  4: ItemType="DeptModel.User"
  5: SelectMethod="GetUsers" UpdateMethod="UpdateUser" DeleteMethod="DeleteUser" InsertMethod="InsertUser">
  6: </FarPoint:SheetView>
  7: </Sheets>

把下面的代码添加到对应的后台页面中。

  1: C#
  2: 
  3:   public IQueryable<User> GetUsers()
  4:   {
  5:     DeptEntities db = new DeptEntities();
  6:     return db.Users.AsQueryable();
  7:   }
  8:  
  9:  
 10:   public bool UpdateUser(string login, string fullName, string email, string description)
 11:   {
 12:     int rowsAffected = -1;
 13:     using (DeptEntities db = new DeptEntities())
 14:     {
 15:       // user should exist in the database in order to be updated
 16:       User found = db.Users.FirstOrDefault(u => u.Login == login);
 17:  
 18:       if (found == null) return false;
 19:  
 20:       // except login name, all other properties of a user can be changed
 21:       found.FullName = fullName; found.Email = email; found.Description = description;
 22:  
 23:       if (ModelState.IsValid)
 24:       {
 25:         rowsAffected = db.SaveChanges();
 26:       }
 27:     }
 28:  
 29:     // there should only be one user updated after running this update (1 row at a time)
 30:     return rowsAffected == 1;
 31:   }
 32:  
 33: 
 34:   public bool InsertUser(string login, string fullName, string email, string description)
 35:   {
 36:     int rowsAffected = -1;
 37:     using (DeptEntities db = new DeptEntities())
 38:     {
 39:       // login name should be unique
 40:       User found = db.Users.FirstOrDefault(u => u.Login == login);
 41:       if (found != null)
 42:       {
 43:         string exceptionMessage = string.Format("Login name should be unique. There is an existing user with the login name of {0}", login);
 44:         throw new InvalidOperationException(exceptionMessage);
 45:       }
 46:       // create new User
 47:       var user = new User()
 48:       {
 49:         Login = login,
 50:         FullName = fullName,
 51:         Email = email,
 52:         Description = description
 53:       };
 54:  
 55:       // add user to model, then commit changes
 56:       if (ModelState.IsValid)
 57:       {
 58:         db.Users.AddObject(user);
 59:         rowsAffected = db.SaveChanges();
 60:       }
 61:     }
 62:  
 63:     return rowsAffected == 1;
 64:   }
 65:  
 66:   public bool DeleteUser(string login)
 67:   {
 68:     int rowsAffected = -1;
 69:     using (DeptEntities db = new DeptEntities())
 70:     {
 71:       User found = db.Users.FirstOrDefault(u => u.Login == login);
 72:       if (found != null)
 73:       {
 74:         db.Users.DeleteObject(found);
 75:         rowsAffected = db.SaveChanges();
 76:       }
 77:     }
 78:     return rowsAffected == 1;
 79:   }
 80: VB
 81: 
 82: Public Function GetUsers() As IQueryable(Of User)
 83: Dim db As New DeptEntities()
 84: Return db.Users.AsQueryable()
 85: End Function
 86: Public Function UpdateUser(login As String, fullName As String, email As String, description As String) As Boolean
 87: Dim rowsAffected As Integer = -1
 88: Using db As New DeptEntities()
 89: ' user should exist in the database in order to be updated
 90: Dim found As User = db.Users.FirstOrDefault(Function(u) u.Login = login)
 91: 
 92: If found Is Nothing Then
 93: Return False
 94: End If
 95: 
 96: ' except login name, all other properties of a user can be changed
 97: found.FullName = fullName
 98: found.Email = email
 99: found.Description = description
100: 
101: If ModelState.IsValid Then
102: rowsAffected = db.SaveChanges()
103: End If
104: End Using
105: 
106: ' there should only be one user updated after running this update (1 row at a time)
107: Return rowsAffected = 1
108: End Function
109: 
110: Public Function InsertUser(login As String, fullName As String, email As String, description As String) As Boolean
111: Dim rowsAffected As Integer = -1
112: Using db As New DeptEntities()
113: ' login name should be unique
114: Dim found As User = db.Users.FirstOrDefault(Function(u) u.Login = login)
115: If found IsNot Nothing Then
116: Dim exceptionMessage As String = String.Format("Login name should be unique. There is an existing user with the login name of {0}", login)
117: Throw New InvalidOperationException(exceptionMessage)
118: End If
119: ' create new User
120: Dim user = New User() With { _
121: .Login = login, _
122: .FullName = fullName, _
123: .Email = email, _
124: .Description = description _
125: }
126: 
127: ' add user to model, then commit changes
128: If ModelState.IsValid Then
129: db.Users.AddObject(user)
130: rowsAffected = db.SaveChanges()
131: End If
132: End Using
133: 
134: Return rowsAffected = 1
135: End Function
136: 
137: Public Function DeleteUser(login As String) As Boolean
138: Dim rowsAffected As Integer = -1
139: Using db As New DeptEntities()
140: Dim found As User = db.Users.FirstOrDefault(Function(u) u.Login = login)
141: If found IsNot Nothing Then
142: db.Users.DeleteObject(found)
143: rowsAffected = db.SaveChanges()
144: End If
145: End Using
146: Return rowsAffected = 1
147: End Function

SpreadJS | 下载试用

纯前端表格控件SpreadJS,兼容 450 种以上的 Excel 公式,具备“高性能、跨平台、与 Excel 高度兼容”的产品特性,备受华为、苏宁易购、天弘基金等行业龙头企业的青睐,并被中国软件行业协会认定为“中国优秀软件产品”。SpreadJS 可为用户提供类 Excel 的功能,满足表格文档协同编辑、 数据填报、 类 Excel 报表设计等业务场景需求,极大的降低企业研发成本和项目交付风险。

如下资源列表,可以为您评估产品提供帮助:

相关产品
推荐相关案例
推荐相关资源
关注微信
葡萄城社区二维码

关注“葡萄城社区”

活字格低代码二维码

关注“活字格低代码”

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态