Reputation: 11
The db table has the columns 'Initials' and 'LastName'. I want to display a ListBox with the text field like "Smith, J". Is there any built-in way to achieve this?
Upvotes: 1
Views: 4707
Reputation: 41
I don't think you should get the solution, instead I will provide you an explanation:
<uc1:CustomTextBox
ID="_txtSomeID"
Text='<%# BindItem.SomeEntity.SomeField +","+BindItem.SomeEntity.SomeField%>'
runat="server" />
Where "SomeEntity" is the table you are fetching and "SomeField" are the columns for the respective table (as some entity)
The way you bind things is completely up to you, but I advice you to use the latest frameworks. Advanced data-binding (OnNeedDataSource) is old and it is not recommended since 4.5 .NET Framework.
Edit: Also note that BindItem is a two-way bind, oppositely you can use just Item.SomeEntity.SomeColumn to do the bind one-way, for visualization purposes. Additionaly, if you are using an ORM you should specify the data source on your grid or master table.
Regards
Upvotes: 0
Reputation: 35613
Use a custom databinding expression.
Consider:
<%# DataBinder.Eval(Container, "DataItem.lastName") + ", " + DataBinder.Eval(Container, "DataItem.lastName") %>
Upvotes: 1
Reputation: 1258
You can do that however you like. You could concatenate the columns in the database, creating a new column called 'LastNameFirstInitial' in addition to returning the raw data. Or you can loop through the result set and add a new ListItem for each record, setting the text and value to whatever combination of the columns you like.
Since you have the data across multiple columns there's no 'build-in way' because you're going to have to combine the columns in either ASP.Net or at the database level.
Upvotes: 0
Reputation: 11
Why don't you concatenate the fields into one single field at the point of returning the data set from SQL? (Or which ever RDMBS you are using).
Much easier to have that kind of processing done on the database service than fandangling with .NET.
Keeps the code easier to maintain as well.
Upvotes: 1