Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2007-11-15
2
Medium Priority
?
1,511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 6

Accepted Solution

by:
ventaur earned 1000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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