1

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?

1

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Don't agree. Formatting should be done on presentation level, not data layer
If you think going through the rigmorole of the following code example you posted before in the above link, rather than organising the data into a singular column on the database end and binding the column to that using a single statement, then I'm not quite sure what you are doing giving out advice.
1

Use a custom databinding expression.

Consider:

<%# DataBinder.Eval(Container, "DataItem.lastName") + ", " + DataBinder.Eval(Container, "DataItem.lastName") %>

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.