Retrieving and displaying data with model binding and web forms
4 stars based on
This tutorial series demonstrates basic aspects of using model binding with an ASP. NET Web Forms project. Model binding makes data interaction more straight-forward binding auto-reporting on remote change dealing with data source objects such as ObjectDataSource or SqlDataSource. This series starts with introductory material and moves to more advanced concepts in later tutorials.
The model binding pattern works with any data access technology. In this binding auto-reporting on remote change, you will use Entity Framework, but you could use the data access technology that is most familiar to you. From a data-bound server control, such as a GridView, ListView, DetailsView, or FormView control, you specify the names of the methods to use for selecting, updating, deleting, and creating data.
In this tutorial, you will specify a value for the SelectMethod. Within that method, you provide the logic for retrieving the data. You can download the complete project in C or VB. The downloadable code works with either Visual Studio or Visual Studio It uses the Visual Studio template, which is slightly different than the Visual Studio template shown in this tutorial.
In the tutorial you run the application in Visual Studio. You can also make the application available over the Internet by deploying it to a hosting provider. Microsoft offers free web hosting for up to 10 web sites in a free Azure trial account. This tutorial also works with Visual Studio but there will binding auto-reporting on remote change some differences in the user interface and project template.
Select the Web Forms template, and leave the other default options. Click OK to setup the project. First, you will make a couple of small changes to customize the appearance of the site. Then, change the header text from Application name to Contoso University. Master, change the navigation links that appear in the header to reflect the pages that are relevant to this site.
You will not need either the About page or the Contact page so those links can be removed. Instead, you will need a link to a page called Students. This page has not been created yet. Now, you'll create the binding auto-reporting on remote change form for displaying student data. Right-click your project, and Add a New Item. You will use Code First Migrations to create objects and the corresponding database tables.
These tables will store information about the students and their courses. The SchoolContext class derives from DbContext, which manages the database connection and changes in the data. These attributes will be used for data validation in this tutorial. To simplify the code for this demonstration project, only these properties were marked with data-validation attributes. In a real project, you would apply validation attributes to all properties that need validated data, such as properties in the Enrollment and Course classes.
In Package Manager Consolerun the command: If the command completes successfully you will receive a message stating migrations have been enabled. Notice that a new file named Configuration. In Visual Studio, this file is automatically opened after it is created. The Configuration class contains a Seed method which enables you to pre-populate the database tables with test data.
Add the following code to the Seed method. You'll need to add a using statement for the ContosoUniversityModelBinding. If you receive an exception when running this command, it is possible that the values for StudentID and CourseID binding auto-reporting on remote change varied from the values in the Seed method. Add those values in the code for seeding the Enrollments table. The database file has been added, but it is currently hidden in the project. Click Show All Files to display the file.
The tables now exist and are populated with data. With data in the database, you are now ready to retrieve that data and display it in a web page. You will use a GridView control to display the data in columns and rows. Within that placeholder, add a GridView control that includes the following code.
There are a couple of important binding auto-reporting on remote change in this markup code for you to notice. First, notice that a value is set for the SelectMethod property in the GridView element.
This value specifies the name of the method that is used for retrieving data for the GridView. You will create this method in the next step. Second, notice that the ItemType property is set to binding auto-reporting on remote change Student class that you created earlier. By setting this value, you can refer to properties of that class in the markup code.
For example, the Student class contains a collection named Enrollments. You can use Item. Enrollments to retrieve that collection and then use LINQ syntax to retrieve the sum of enrolled credits for each student. In the code-behind file, you need to binding auto-reporting on remote change the method that is specified for the SelectMethod value. Then, add the following method. Notice that the name of this method matches the name you provided for SelectMethod. The Include clause improves the performance of this query but is not essential for the query to work.
Without the Include clause, the data would be retrieved using lazy loading, which involves sending a separate query to the database each time related data is retrieved. By providing the Include clause, the data is retrieved using eager loading, which means all of the related data is retrieved through a single query of the database. In cases where most of the related data is not be used, eager loading can be less efficient because more data is retrieved.
However, in binding auto-reporting on remote change case, eager loading provides the best performance because the related data is displayed for each record. By default, the data is sorted by the values of the property marked as the key. You can add an OrderBy clause to specify a different value for sorting. In this example, the default StudentID property is used for sorting. In the Sorting, Paging, and Filtering Data topic, you will enable the user to select a column for sorting.
Run your web application, and navigate to the Students page. The Students page displays the following student information.
When working through this tutorial series, you can simply copy the code from the tutorial to your project. However, one disadvantage of this approach is that you may not become aware of the feature provided by Visual Studio to automatically generate code for model binding methods. When working on your own projects, automatic code generation can save you time and help you gain a sense of how to implement an operation.
This section describes the automatic code generation feature. This section is only informational and does not contain any code you need to implement in your project.
Visual Studio not only creates a method in the code-behind with the proper signature, but also generates implementation code to assist you with performing the operation. If you first set the ItemType property before using the automatic code generation feature, the generated code will use that type for the operations. For example, when setting the UpdateMethod property, the following code is automatically generated:.
Again, the above code does not need to be added to your project. In the next tutorial, you will implement methods for updating, deleting and adding new data. In this tutorial, you binding auto-reporting on remote change data model classes and generated a database from those classes.
You filled the database tables with test data. You used model binding to retrieve data from the database, and then displayed binding auto-reporting on remote change data in a GridView.
In the next tutorial in this series, you will enable updating, deleting, and creating data. The feedback system for this content will be changing soon. Old comments will not be carried over. If content within a comment thread is important to you, please save a copy.
For more information on the upcoming change, we invite you to read our blog post. Software versions used in the tutorial Microsoft Visual Studio or Microsoft Visual Studio Express for Web This binding auto-reporting on remote change also works with Visual Studio but there will be some differences in the user interface and binding auto-reporting on remote change template.
What you'll build In this tutorial, you'll: Build data objects that reflect a university with students enrolled in courses Build database tables from the objects Populate the database with test data Display data in a web form Set up project In Visual Studiocreate a new ASP. Master as the master page for the new web form.
Create the data binding auto-reporting on remote change and database You will use Code First Migrations to create objects and the corresponding database tables. In the Models folder, add a binding auto-reporting on remote change class named UniversityModels.
You will use the tools for Code First Migrations to set up a database based on these classes. SchoolContext If binding auto-reporting on remote change command completes successfully you will receive a message stating migrations have been enabled, Notice that a new file named Configuration.
Models; internal sealed class Configuration: In binding auto-reporting on remote change Package Manager Console, run the command add-migration initial.
Then, run the command update-database.