Saturday, March 31, 2012

the best way display data from dropdownlist?

OK..i have a field called - areaID,

i would like to make it to display areaNAME instead of ID, so a human can understand.

What i have done:

1. Converted that field into template;

2. For Edit mode - i made drop down list where a user can choose areaName instead of areaID, though areaID is bounded to the field.

3. For Item mode - ? my question:

i don't want to make the same dropdown list display, it looks not appropriate, - i would like to have 'label' like component to display my areaName on areaID field, how to do it?

I could probably create some function or stored procedure to return areaName, and as a parameter use areaID, but i don't want to complecate matters too much...

I know it had been done 100 times...but not by me...

anyone can give a hint?

thanks for your help!!

I'm not sure why you wouldn't just return the areaname in the query/sp that fills the grid, and use a Label or Literal in your ItemTemplate. After all, the value for that row doesn't really come from the dropdownlist; it comes from the datasource for the parent control.


Hi Peter, I think I have the same issue in that I would like to display descriptive names for domain data in the ItemTemplate, but in order to bind a dropdown in the EditTempate I also need the ID field. Are you suggesting having both the IDand description fields available in the datasource and then binding ItemTemplate label to the description field and the EditTemplate dropdown to the ID field? Sorry if I'm slow, I'm new to .NET in general


Thanks,

- Ben


I am not sure you are asking the right question, because it sounds like you are using a GridView, or other data control. But dropdowns have two properties, DataTextField and dataValueField where you set the field names to display in a drop-down.

That's not what I'm getting at; In a FormView I would like to show a label control in the "view" mode (ItemTemplate) and switch to a dropdown control in the "edit" mode (EditTemplate). My data table currently stores only the ID/foreign key of the domain table as it has been normalized. Now, this arrangement works perfectly with the dropdown as it is populated with the domain dataset and then it's selected row is bound to the ID field in my formview's dataset. Fine.

But in "view" mode I want to add alabel control that shows the user-friendly descriptive text from the domain table instead of a cryptic ID. As stated by the original poster, I could always write a function to "lookup" the domain description using the ID as a key but I'm thinking that's not the in the spirit of the declarative syntax and it's not efficient.

What I think was proposed was to join to the domain table in my source query and get the domain description and have it available along with the ID so I can bind to either one as needed. Was that the idea?


Yes, that's it. Just join on the ID field and pull the name as part of your datasource.


Thanks!

0 comments:

Post a Comment