如何在同一个下拉列表 ASPX 中添加各种 SQL 列

How to add various SQL columns in the same dropdownlist ASPX

例如,数据库有这些列:

Name   Type   Place

我已经在下拉列表中显示了所有名称;如何在同一个下拉列表中显示所有三列 - 名称、类型、地点?

这是我得到的

<tr>
    <td align="left" valign="top">Name:</td>
    <td align="left" colspan="3" style="height: 21px" valign="top">
    <asp:DropDownList ID="DropD_Name" runat="server" 
         BackColor="White" AppendDataBoundItems="true" 
         DataSourceID="SqlDS_Name" DataTextField="Name" DataValueField="Name" 
         ForeColor="Black" 
         SelectedValue='<%# Bind("Name") %>' Width="290px">
        <Items>
            <asp:ListItem Selected="True"></asp:ListItem>
        </Items>
    </asp:DropDownList>
    </td>
</tr>

SqlDS_Name:

<asp:SqlDataSource ID="SqlDS_Name" runat="server" 
     ConnectionString="<%$ ConnectionStrings:BlablablaConnectionString %>"
     SelectCommand="SELECT Name, Type, Place FROM dbo.vwnames">
</asp:SqlDataSource>

感谢您的帮助 最后这是查询的解决方案

SELECT CONCAT (Name,' - ',Type,' - ',Place) AS TextColumn,Name AS NameInfo FROM dbo.vwnames
SELECT Name, Type, Place FROM dbo.vwnames

更改为:

SELECT Name + ',' + Type + ',' + Place AS TextColumn,Name FROM dbo.vwnames

结果:

<asp:SqlDataSource ID="SqlDS_Name" runat="server" 
     ConnectionString="<%$ ConnectionStrings:BlablablaConnectionString %>"
     SelectCommand="SELECT Name + ',' + Type + ',' + Place AS TextColumn,Name FROM dbo.vwnames">
</asp:SqlDataSource>

DropDownList更改为:

<asp:DropDownList 
   ID="DropD_Name"
   runat="server" 
   BackColor="White" 
   AppendDataBoundItems="true"
   DataSourceID="SqlDS_Name" 
   DataTextField="TextColumn" 
   DataValueField="Name" 
   ForeColor="Black" 
   SelectedValue='<%# Bind("Name") %>' 
   Width="290px">
  <Items>
    <asp:ListItem Selected="True"></asp:ListItem>
  </Items>
</asp:DropDownList>

你可以试试这个。但是格式化会很糟糕

SelectCommand="SELECT CONCAT(Name,' ',Type,' ',Place) AS Name FROM dbo.vwnames"

你可以尝试使用string leftpad or rightpad 和proportional fonts 来改进它,但这不在这个问题的范围内。最好尝试像 listview 或 gridview 这样的东西。