views:

362

answers:

3

If you bind a control in a FormView using two way binding (such as Text='<% #Bind("FieldName") %>'), how do you retrieve the field name "FieldName"? There are several things I want to do with this information; for example, I have a database table that contains a dump of all the field definitions from ERWin and I want to programmatically create some sort of context-sensitive help for each individual field (there are hundreds spread across dozens of forms).

This is pretty much an exact duplicate of a question asked a year ago but the answer didn't make much sense to me. First, the answer seemed to be for a GridView and not a FormView (e.Row.Cells[0] gave it away). Second, what does SortExpression have to do with anything? Third, it mentions an event argument, but for what event? In OnDataBound, EventArgs e is empty.

A: 

An easy way of doing this is to programatically assign the name of the data field to the Tooltip property of your Formview controls, then the data field names will be shown to the user as they mouse over these controls. If you want a more specific answer, please specify if this is what you are trying to accomplish.

Ricardo
What I am trying to accomplish is in the first sentence of the first paragraph -- get the name of a two-way bound field at runtime. The rest is just one example of what I intend to do with the field name after I get it.
pjabbott
A: 

you can get using follwing code...

protected void frm_DataBound(object sender, EventArgs e)
{
    if (frm.DataItem != null)
    {
        hdfImageID.Value = ((DataRowView)frm.DataItem)["imageResID"].ToString();
    }
}
Muhammad Akhtar
That's going to get me the value of the field for the current record, not the field name itself. I'm looking to get the column name (in your case, "imageResId") given the ID of the text box control.
pjabbott
+1  A: 

There does not appear to be any way to get at this information from a FormView, as the column name is not stored at the level you want it.

However, I must admit that I do not understand why you want to retrieve at runtime something that you know at compile time. Why is it not possible to just write the code you need? Even if you want your code to be more generic, you can create a dictionary of control names and their associated bound column names to pass to your class that does whatever it needs to do.

Regarding the answer to your last question - the GridView stores the column name in the SortExpression property, so that it knows what column to sort by when the user resorts the grid. Hence, in a GridView, you can access the column name through the SortExpression.

Jason Berkan
I may have to resort to creating a dictionary, but this is an application with about three dozen web forms, hundreds of tables, and thousands of fields. It would take a solid week just to do all the typing involved in linking field name to control name, and then I would have yet another data structure to maintain. I was hoping for a simple way out.
pjabbott
You could write a quick Python script to rip through your ASPX files and generate the required code for each page.
Jason Berkan
+1 Jason. Solving problems with code is exactly what programming is all about.
Charlie Brown
Well, here and Google says it can't be done, so I guess this is the answer.
pjabbott
I was still researching this, and not Google, but Reflector showed that it is indeed not possible. The main reason: the `DataBinder.Eval` methods (where `Eval` is mapped to), which later uses `TypeDescriptor.GetProperties()` to get the properties and the values, are not cached. If they were cached one way or another, you could use reflection to get the information. In the case of `GridView` the names are stored, in the case of `FormView` they are not. The best solution is a global search/replace from `Eval(prop)` to `Util.StoreProp(Eval(prop), prop)`, which shouldn't be too hard to do.
Abel