Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have multiple values in a DataValueField?

Tags:

c#

asp.net

Is it possible to put multiple values in a DataValueField?

For example, with the following code:

<asp:DropDownList ID="ixTeam" CssClass="ixTeam" runat="server" DataSourceID="dsixTeam" 
 DataValueField="TeamID" DataTextField="TeamName" AppendDataBoundItems="true">

Is it possible for me to put two values (e.g. TeamID + "|" + TeamTypeID) into the DataValueField?

like image 587
Jin Yong Avatar asked Jul 30 '12 04:07

Jin Yong


3 Answers

You can't combine two fields in DataValueField, but you can try couple of things.

  1. You can build your SQL query with concatenation, (if you are getting the data from database)

SQL query:

Select (TeamID +"|"+ TeamTypeID) as CombinedTeam, .....`

and then set

DataFieldValue = "CombinedTeam"

Or

  1. You can use LINQ query to create an anonymous type based on concatenation of two fields.

for example:

var query = from t in yourDataSource
            select new 
            {
                 CombinedTeam = t.TeamID + "|" + t.TeamTypeID,
            } 

Then specify the data source for the drop down to the LINQ query, and specify the DataFieldValue

yourDropDownList.DataSource = query;
yourDropDownList.DataFieldValue = "CombinedTeam";
like image 108
Habib Avatar answered Nov 06 '22 21:11

Habib


The short answer is you cannot.

Instead, create a calculated column in your dataset which has TeamID + '|' + TeamTypeID for its expression (you can also do this when you fetch the data adding a formatted display column to your select query, assuming you are using a database), then bind on that column instead.

like image 4
lc. Avatar answered Nov 06 '22 20:11

lc.


Yes, it can be done. Either format your data when retrieving from SQL server as following:

Select TeamID + ' | ' + TeamTypeID CombinedColumn

Now set the DataValueField=CombinedColumn

like image 3
Munawar Avatar answered Nov 06 '22 20:11

Munawar