DropDownList - DataTextField上的多个项目

时间:2012-11-05 16:40:13

标签: c# asp.net webforms

我想从数据库(姓氏和姓名)中加上DataTextField个以上的项目。我怎么能这样做?
当然,DataTextField="surname + name"不起作用,但有没有可能把这2个项目放在一起?

有我的代码:

<asp:DropDownList runat="server" ID="dllSpecialist" DataValueField="iduserspecialist" DataTextField="surname" AutoPostBack="true" OnSelectedIndexChanged="dllSpecialist_IndexChanged" AppendDataBoundItems="true">
                        <asp:ListItem Text="" Value="0"></asp:ListItem>
</asp:DropDownList>
代码背后的代码:

    if (!IsPostBack)
    {
        dllSpecialist.DataSource = tUserSpecialistBO.getAllSpecialist();
        dllSpecialist.DataBind();
       (..)
    }

sql方法:

public static DataSet getAllSpecialist()
{

    sql = "select * from tuserspecialist where del='false' and name!=''";
    return SQLTools.getDataSet(sql);
}

2 个答案:

答案 0 :(得分:3)

更改您的SQL语句:

sql = "select surname + ' ' + name as FullName, iduserspecialist from tuserspecialist where del='false' and name!=''";

然后更改您的属性以指定新数据项:

DataTextField="FullName"

答案 1 :(得分:0)

您可以执行类似

的操作
SELECT name + ' ' + surname AS Fullname FROM tuserspecialist WHERE del='false' AND name!=''"

然后设置DataTextField="Fullname";