Solved

How can I access the values of ObjectDataSource from Code-Behind?

Posted on 2007-11-15
2
1,482 Views
Last Modified: 2013-11-07
I placed an ObjectDataSource in my asp.net page (VB.NET) that is bound to a a FormView.  For a simple project this works fine as I use Conainter.DataItem to display the value in a label such as:

<asp:Label ID="lblName" runat="server" Text='<%# Container.DataItem.ProductName %>'></asp:Label>

My question now is - how to I access all the values returned from the ObjectDataSource's SelectMethod from Code-Behind.  There are many reasons why I may want to do this such as conditional formatting, setting variables, cookies, session variables, hiding/displaying panels on the page, etc.

Please let me know how this can be done properly.

Thank you.
<asp:Label ID="lblName" runat="server" Text='<%# Container.DataItem.ProductName %>'></asp:Label>

Open in new window

0
Comment
Question by:blueswitch
2 Comments
 
LVL 6

Accepted Solution

by:
ventaur earned 250 total points
ID: 20291702
If you want to do things inside the FormView after the data is bound, use the FormView's DataBound event and reference it's DataItem object after casting to the appropriate type.
<asp:FormView ID="FormView1" runat="server" DataSourceID="ObjectDataSource1" OnDataBound="FormView1_DataBound">
...
 
 
Protected Sub FormView1_DataBound(sender As Object, e As EventArgs)
	' Get the data item after type casting it appropriately.
	' * Cast to DataRowView if using a DataSet or DataTable.
	Dim Data As Product = TryCast(FormView1.DataItem, Product)
	If Not (Data Is Nothing) Then
		' Change the product name label's style depending on if the product is active or not.
		Dim lblName As Label = DirectCast(FormView1.FindControl("lblName"), Label)
		Dim IsActive As Boolean = Data.IsActive
		If IsActive Then
			lblName.CssClass = "ActiveProduct"
		Else
			lblName.CssClass = "InactiveProduct"
		End If
	End If
End Sub

Open in new window

0
 

Author Closing Comment

by:blueswitch
ID: 31409392
Works like a charm.  Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question