Practical ASP.NET

Displaying and Updating Dynamic DataBound Columns with ObjectDataSource

Peter continues his series on integrating cascading DropDownLists with databound controls. But this time, he looks at what's different when you're using the ObjectDataSource.

In two previous columns I discussed the problem that generates this message:

"Databinding methods such as Eval(), XPath() and Bind() can only be used in the context of a databound control."

In the first column of the series ("Integrating Dynamic DataBound Columns with SqlDataSource"), I laid the groundwork for a solution and showed how to get the data to display correctly in both Edit and Insert modes with the SqlDataSource. In the second column ("Updating Dynamic DataBound Columns with SqlDataSource"), I looked at what you needed to do handle updates and inserts, again with the SqlDataSource. In this column, I'll cover the differences when working with the ObjectDataSource.

Displaying Data
As with the SqlDataSource, the first two steps are to: 1) remove the databinding from the DropDownList that actually displays the value from the DataSource, and 2) add code to the FormView's DataBound event to move the data into the control on the page from the FormView's DataItem property. With the Object DataSource, the DataItem must be cast to whatever object type the FormView is displaying. If the FormView is bound to an OrderDetails object, for instance, then you must case the DataItem to an OrderDetails object, as this code does:

Dim ordd As OrderDetail
ordd = CType(Me.FormView1.DataItem, OrderDetail)

After that, it's just a matter of working with the property on the object that the DropDownList should have been bound to. For my example, that's the ProductName property:

ddl.SelectedValue = ordd.ProductName 

Inserts and Updates
For inserts, the code for the ObjectDataSource is identical to the code for the SqlDataSource. For updates, the code is different and simpler for the ObjectDataSource: In the FormView's ItemUpdating event (which fires just before the update is performed), you just need to set the value of the unbound field from the DropDownList through the NewValues collection that's passed to the event on the e parameter. The three lines of code shown in Listing 1 do the job.

One Last Problem
Back in the first column of this series, I ensured that the value inserted into the DropDownList was in the list of values for the control by adding the value to the DropDownList's Items collection. There are at least three problems with that solution.

First, the value pulled from the FormView's DataSource will often be the "primary key" value for the data and not the text value that would normally be displayed in the DropDownList. For instance, for a list of Products, you'll find that you typically have the ProductId but not the ProductName -- and it's the ProductName that should be displayed in the DropDownList. Second, the DropDownList will now have an inconsistent list of values: Most of the values will be driven by the category DropDownList but the list will also have the one value you've added which will be from some other category. Finally, of course, the item displayed in the FormView will be out of sync with the category displayed in the category DropDownList.

A better solution is to force the category DropDownList to the correct category for the value in the DataSource. Essentially, the process is to use the "primary key" value retrieved through the DataSource to retrieve the complete record. Once that record is retrieved, you can use data from it to set the current value in the category DropDownList. The final step is to refresh the DropDownList so that it's filled with the matching values from the category DropDownList.

In my case, because I use the Factory Method pattern to return Lists of my business objects, typical code looks like Listing 2 (in this example, the category DropDownList is called DropDownList2).

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

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube