简体   繁体   English

使用 DataTextField 属性设置列表框格式

[英]Listbox formatting using DataTextField property

I have a list box in my aspx page:我的 aspx 页面中有一个列表框:

<asp:ListBox ID="lstTreatmentProvider" runat="server" SelectionMode="Multiple" Width="175px"
                                        Height="81" CssClass="SingleColumnlist" DataSourceID="dtsTreatmentProviders" DataTextField="FirstName" DataValueField="ServiceId"></asp:ListBox>

I am using this datasource for listbox:我将此数据源用于列表框:

<asp:ObjectDataSource ID="dtsTreatmentProviders" runat="server" SelectMethod="GetAllTreatmentProviders"
                        TypeName="Pc.PrecisionCare2.BLL.Administration.TreatmentProvider.TreatmentProviderBO"
                        SortParameterName="sortExpression"></asp:ObjectDataSource>

As you can see, I am using in list box, DataTextField="FirstName" because my datasource returns some data of treatment provider including first name, last name etc.如您所见,我在列表框中使用DataTextField="FirstName"因为我的数据源返回一些治疗提供者的数据,包括名字、姓氏等。

I want to have my list box as containing First Name + Last Name Can I do this somehow using DataTextField property?我想让我的列表框包含First Name + Last Name我可以使用 DataTextField 属性以某种方式做到这一点吗?

PS: I do not want to do this in cs file. PS:我不想在cs文件中这样做。 I want something in aspx.我想要一些在 aspx 中的东西。

Thanks in advance.提前致谢。

You can get the First Name + Last Name in your SQL query, if you don't want to do it in code behind.如果您不想在后面的代码中执行此操作,您可以在 SQL 查询中获取First Name + Last Name eg..例如..

Select ([First Name] + [Last Name]) as [First Name],... from TableName

You can try adding a Public property to your TreatmentProviderBO like below您可以尝试向TreatmentProviderBO添加公共属性,如下所示

public String FullName
{
    get { return FirstName + " " + LastName ;}
}

You can set the DataTextField to FullName.您可以将 DataTextField 设置为 FullName。

If you can't edit the BO, I think you can use Extension Methods, though I'm not very sure whether you can give a method name to DataTextField.如果你不能编辑 BO,我认为你可以使用扩展方法,虽然我不太确定你是否可以给 DataTextField 一个方法名称。 You may try.你可以试试。

Would this post be helpful for you?这篇文章对你有帮助吗?

Bind drop down list with Dictionary 将下拉列表与字典绑定

I think you could use the anonymous type too.我认为您也可以使用匿名类型。

If you don't want to change the query as you are sticking with LINQ try the following one如果您不想更改查询,因为您坚持使用 LINQ,请尝试以下一个

  var aa = (from Varible in Collection select new { name = Varible.FirstName + Varible.LastName }).ToList();

Now set the DataTextField Property to "name".现在将 DataTextField 属性设置为“名称”。 Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM