views:

322

answers:

3

I'd like to set the text of two labels to values found in a FormView on a page (whose data comes from an SQLDataSource.)

What's the best way to do this? I'm thinking of using the DataBound event for the FormView to set the label text to the value of a field in the FormView, or of using the SQLDataSource Selected event to set the labels to values retrieved by the query. Could I use the Page_Load event in conjunction with the FormView?

The FormView only displays one of the two values, though the other value is retrieved by the SQLDataSource.

I'm unfamiliar with accessing the data structures behind these controls but figure the data is there so I might as well use it rather than run the same SQL query twice.

My question then is which event do I use, which control do I access the data from, and how do I access the data from that control?

A: 

If you're just trying to set a label, just set the label at the Page_load event:

myLabel.Text = "someValue";

JonH
But how do I get the value - if I retrieve it from the FormView control, can I access values that aren't displayed? Does the SQLDataSource store the query results in some data structure that I could access?
Duke
You get could the values by using the FindControl Method of the FormView
citronas
+3  A: 

I'd use the OnDataBound event and get the value from the underlying datasource using:

lblExample.Text = ((DataRowView)((FormView)sender).DataItem)["fieldName"].ToString();

Hope it helps. // CeriQ

CERIQ
I found a similar solution after asking my question. I tried using the Page_Load event and just accessing the FormView control directly (rather than as the sender in an associated event.) In the context of the Page_Load event FormView.DataItem is returning null, while the debugger shows it actually contains a DataRowView with the data I want to access.
Duke
Never mind, on subsequent reloads it looks like that was a fluke.
Duke
In the page_load there's no guarantee that the formview will be populated. By using the OnDataBound event you'll only trigger once you've got data, or the data has changed(been repopulated). You don't need to use sender ofcourse, you can just exchange ((FormView)sender) with the id of the FormView control.
CERIQ
A: 

I am using CERIQ's answer, but am not having any luck. I converted the code to vb.net for my project and it looks like this:

   Protected Sub fvClients_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fvClients.DataBound

    lblClientName.Text = (DirectCast((DirectCast(sender, FormView)).DataItem, DataRowView))("FirstName" + " " + "LastName").ToString()
End Sub

However, no dice. Am I doing something wrong? Also, please note my lbClientName control resides outside of the formview, could this be the issue?

Please help!

Brett
Please note, I fixed this as follows:lblClientName.Text = CType(fvClients.FindControl("FirstNameLabel"), Label).Text + " " + CType(fvClients.FindControl("LastNameLabel"), Label).Text
Brett