views:

11

answers:

1

Hi -

The method has a return type of collection, i am calling the method from the aspx page in order to populate a drop down -> ddlDropDown. I am binding he DataTextField of the ddlDropDown with the BusinessEntity.Name and the DataValueField with BusinessEntity.Id, the business entity contains another id which is BusinessEntity.ProductId. I need to use the ProductId of the value selected in the drop down list in the code behind. Can you help suggest how i can do so?

One possible way could be to call the method in the page_Load on the code behind and save the collection in a hidden variable and when required do a loop through in the hidden variable and retrieve the selected value Product Id.

Your thoughts please.

Thanks

A: 

You could certainly accomplish this using a hidden field.

Here's another idea: bind the DataValueField to a special derived string, containing the BusinessEntity.Id AND the BusinessEntity.ProductId.

In other words, concatenate the BusinessEntity.Id and BusinessEntity.ProductId into a single string, seperated by the pipe ("|") symbol for example.

E.g to bind:

ddlFoo.DataValueField = string.Format("{0}|{1}", "Id", "ProductId");

Then to retrieve selected item:

var id = ddlFoo.SelectedValue.Split("|")[0];
var productId = ddlFoo.SelectedValue.Split("|")[1];

Saves you looping/matching. Again, its not ideal, but then again neither is binding multiple values types to a dropdownlist.

RPM1984