Spread for ASP.NET 8.0 Product Documentation
Model Data Binding in ASP.NET 4.5

Spread for ASP.NET supports model data binding as provided by ASP.NET 4.5. The Spread component provides the ItemType property, as well as the SelectMethod, UpdateMethod, InsertMethod, and DeleteMethod properties for working with model binding.

Model data binding only takes effect in .NET 4.5 or higher; if you try to use model data binding in another .NET environment, nothing happens. If you use model data binding and the DataSourceID property in the same project, the component throws an exception. If you use the DataSourceID and set one of the model data binding properties, such as SelectMethod or UpdateMethod, the component will also throw an exception.

The ItemType property indicates the type of data item used in model data binding. By default, it is empty. Set this property to use strongly typed data binding. If you set the ItemType property and set some sheets' SelectMethod property, the .NET framework will try to cast the data items to the type declared by the ItemType property. Therefore, you should set the ItemType and SelectMethod properties to the same data types, or set the SelectMethod to a parent data type. If you do not, the component will throw an exception.

The SelectMethod, UpdateMethod, InsertMethod, and DeleteMethod properties set the name of the method to use to get, update, insert, or delete a data item in the data source. Note the following when using these properties.

Using Code

  1. Set up the data source. See the example code below.
  2. Bind the Spread component to the data source. For more information, see Binding to a Data Source.
  3. If you want to do so, set the ItemType property.
  4. Set the SelectMethod, UpdateMethod, InsertMethod, and DeleteMethod properties to the names of methods you provide in your project for handling these data binding tasks.

Example

This example code illustrates the model data binding properties.

The following code is added to the .aspx page:

<Sheets>
  <FarPoint:SheetView SheetName="Sheet1"
    AllowDelete="true" AllowInsert="true"
    ItemType="DeptModel.User"
    SelectMethod="GetUsers" UpdateMethod="UpdateUser" DeleteMethod="DeleteUser" InsertMethod="InsertUser">
  </FarPoint:SheetView>
</Sheets>

Code is added to the .cs or .vb page to create the methods referred to in the .aspx page, as shown in the following sample.

C#
Copy Code
  public IQueryable<User> GetUsers()
  {
    DeptEntities db = new DeptEntities();
    return db.Users.AsQueryable();
  }
 
  public bool UpdateUser(string login, string fullName, string email, string description)
  {
    int rowsAffected = -1;
    using (DeptEntities db = new DeptEntities())
    {
      // user should exist in the database in order to be updated
      User found = db.Users.FirstOrDefault(u => u.Login == login);
      if (found == null) return false;
      // except login name, all other properties of a user can be changed
      found.FullName = fullName; found.Email = email; found.Description = description;
      if (ModelState.IsValid)
      {
        rowsAffected = db.SaveChanges();
      }
    }
    // there should only be one user updated after running this update (1 row at a time)
    return rowsAffected == 1;
  }

  public bool InsertUser(string login, string fullName, string email, string description)
  {
    int rowsAffected = -1;
    using (DeptEntities db = new DeptEntities())
    {
      // login name should be unique
      User found = db.Users.FirstOrDefault(u => u.Login == login);
      if (found != null)
      {
        string exceptionMessage = string.Format("Login name should be unique. There is an existing user with the login name of {0}", login);
        throw new InvalidOperationException(exceptionMessage);
      }
      // create new User
      var user = new User()
      {
        Login = login,
        FullName = fullName,
        Email = email,
        Description = description
      };
      // add user to model, then commit changes
      if (ModelState.IsValid)
      {
        db.Users.AddObject(user);
        rowsAffected = db.SaveChanges();
      }
    }
    return rowsAffected == 1;
  }
  public bool DeleteUser(string login)
  {
    int rowsAffected = -1;
    using (DeptEntities db = new DeptEntities())
    {
      User found = db.Users.FirstOrDefault(u => u.Login == login);
      if (found != null)
      {
        db.Users.DeleteObject(found);
        rowsAffected = db.SaveChanges();
      }
    }
    return rowsAffected == 1;
  }
VB
Copy Code

Public Function GetUsers() As IQueryable(Of User)
 Dim db As New DeptEntities()
 Return db.Users.AsQueryable()
End Function
 
Public Function UpdateUser(login As String, fullName As String, email As String, description As String) As Boolean
 Dim rowsAffected As Integer = -1
 Using db As New DeptEntities()
  ' user should exist in the database in order to be updated
  Dim found As User = db.Users.FirstOrDefault(Function(u) u.Login = login)

  If found Is Nothing Then
   Return False
  End If

  ' except login name, all other properties of a user can be changed
  found.FullName = fullName
  found.Email = email
  found.Description = description

  If ModelState.IsValid Then
   rowsAffected = db.SaveChanges()
  End If
 End Using

 ' there should only be one user updated after running this update (1 row at a time)
 Return rowsAffected = 1
End Function

Public Function InsertUser(login As String, fullName As String, email As String, description As String) As Boolean
 Dim rowsAffected As Integer = -1
 Using db As New DeptEntities()
  ' login name should be unique
  Dim found As User = db.Users.FirstOrDefault(Function(u) u.Login = login)
  If found IsNot Nothing Then
   Dim exceptionMessage As String = String.Format("Login name should be unique. There is an existing user with the login name of {0}", login)
   Throw New InvalidOperationException(exceptionMessage)
  End If
  ' create new User
  Dim user = New User() With { _
   .Login = login, _
   .FullName = fullName, _
   .Email = email, _
   .Description = description _
  }

  ' add user to model, then commit changes
  If ModelState.IsValid Then
   db.Users.AddObject(user)
   rowsAffected = db.SaveChanges()
  End If
 End Using

 Return rowsAffected = 1
End Function

Public Function DeleteUser(login As String) As Boolean
 Dim rowsAffected As Integer = -1
 Using db As New DeptEntities()
  Dim found As User = db.Users.FirstOrDefault(Function(u) u.Login = login)
  If found IsNot Nothing Then
   db.Users.DeleteObject(found)
   rowsAffected = db.SaveChanges()
  End If
 End Using
 Return rowsAffected = 1
End Function

See Also

 

 


Copyright © GrapeCity, inc. All rights reserved.

Support Options | Documentation Feedback