Practical ASP.NET

Creating Master/Detail and Update Pages with Entity Framework

After using EF to drive a series of drop-down lists, Peter moves on to the next step: using EF to build a Master/Detail page.

In previous columns ("Integrating Entity Framework with an ASP.NET Web Site," "Extending an Entity Framework Model -- and Some Limitations" and "Using the Entity Model to Create an ASP.NET Page"), I've built a simple Entity Framework model and used it to drive a series of drop-down lists both with and without the AJAX-enabled UpdatePanel. With this column, I'm moving on to the next obvious step: Creating a Master/Detail page with updating.

In previous columns, I created an Entity diagram by starting with a blank diagram and handcrafting my entities. This time, I added an ADO.NET Entity Data Model named Northwind.edmx and, once my .EDMX file was added (and opened), I right-clicked on its design surface and selected "Update Model from Database." In the Update Wizard dialog that appeared, I selected the Northwind database and added the Categories and Products tables. With my Entity diagram completed, I built the Web site to make the model available to the other components in the application.

Creating the Page
To begin the page, I dragged a ScriptManager and an AJAX UpdatePanel onto the page. With the page's infrastructure in place, I then dragged on an EntityDataSource and selected Configure Data Source. In the resulting wizard, I selected my Entity model (now called NorthwindContainer) and clicked the Next button. On the second page, I selected Categories from the EntitySetName drop-down list (if your tables don't appear, it just means that you forgot to rebuild the Web site after adding the tables to your Entity model). Once I selected a table, I checked off the options to turn on updates and picked the fields I wanted.

With the EntityDataSource in place, I dragged a GridView onto the UpdatePanel, connected it to the EntityDataSource and turned on editing/deleting/paging/selecting. A quick press of the F5 key showed that I was displaying all the Categories table data with all the features of the GridView without any page postbacks.

The bad news: At this point, I started getting regular JavaScript errors when I clicked on the Select button on the GridView. Since my page wasn't done yet, I shrugged that off and plunged on.

I dragged a second EntityDataSource onto the page and tied it to the Products table in my Entity model. I added a second GridView and tied it to the new EntityDataSource to display my Products data. At this point I noticed a second anomaly: While my Categories GridView still show its columns as DataBoundColumn0, DataBoundColumn1, etc., my Products GridView showed the field names for the Products table (e.g., ProductId, Description). Interestingly, the foreign key in the Products table that linked back to the Categories table appeared as Categories.CategoryID. On the other hand, I don't really care what the column headers look like in design view as long as everything's OK at run time. So I continued on.

As I did in my earlier column with the drop-down lists, I added a Where clause to the Products DataSource that tied the Categories.CategoryID field to the SelectedValue property of the Categories GridView. I also set the DataKeyNames property of the Categories GridView to the CategoryID field so that field's value would be returned through the GridView's SelectedValue property.

Another press of the F5 key and I had a working Master/Detail page. I could update Categories data and Products data. When I selected a row in the Categories table, only the corresponding rows in the Products table were displayed (and, mysteriously, my JavaScript error on selecting rows had gone away).

So what's missing? Well, so far, I have yet to drag a DynamicDataManager onto the page. Which raises the question: Do I need Dynamic Data? I've got everything I wanted from the EntityDataSource with my data access layer objects now being automatically generated for me (and I haven't even begun to investigate my ability to customize those objects). But I have yet to drag a DynamicDataManager onto the page.

I'm going to return to Dynamic Data in future columns. However, now that I've got a working page, I'm going to spend the next few columns looking at the other new feature in SP1 for .NET 3.5 -- routing.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events