在 DropDownList 中显示数据库中的更多列

发布于 2024-11-14 21:14:53 字数 567 浏览 6 评论 0原文

如何在asp.net中的DropDownList中显示两列?

SqlDataSourceNamePeop.SelectCommand = "select name,forename from people";
DropDownListCaptain.DataTextField = "name";

像这样它只显示名称。但是我怎样才能显示名字+名字呢?

<asp:DropDownList ID="DropDownListPeople" 
                  runat="server" 
                  DataSourceID="SqlDataSourceNamePeop">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceNamePeop" 
                   ConnectionString="xxxxx" 
                   runat="server">
</asp:SqlDataSource>

How can I display two columns in the DropDownList in asp.net?

SqlDataSourceNamePeop.SelectCommand = "select name,forename from people";
DropDownListCaptain.DataTextField = "name";

Like this it displays only the names. But how can i display the names+forenames?

<asp:DropDownList ID="DropDownListPeople" 
                  runat="server" 
                  DataSourceID="SqlDataSourceNamePeop">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceNamePeop" 
                   ConnectionString="xxxxx" 
                   runat="server">
</asp:SqlDataSource>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

人间☆小暴躁 2024-11-21 21:14:53

最简单的方法是像这样创建 sql 查询:

SqlDataSourceNameJuc.SelectCommand = "select name + ' ' +forename as FullName from people";

然后将此自定义属性绑定到下拉列表,如下所示:

DropDownListCaptain.DataTextField = "FullName";

让我知道它是否解决了您的问题,谢谢。

The easiest way is to make your sql query like this:

SqlDataSourceNameJuc.SelectCommand = "select name + ' ' +forename as FullName from people";

then bind this custom property to your drop down list like this:

DropDownListCaptain.DataTextField = "FullName";

Let me know if it fixed your problem, thanks.

春夜浅 2024-11-21 21:14:53

将您的选择命令更改为

"Select name + ',' + forename as 'name' from people"

Change your select command to

"Select name + ',' + forename as 'name' from people"
后eg是否自 2024-11-21 21:14:53
<asp:DropDownList ID="DropDownList9" runat="server" 
            DataSourceID="SqlDataSource3" DataTextField="**CALL_NAMESURNAME**" 
            DataValueField="CALL_NAME">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:McCarthyConnectionString %>" 
            SelectCommand="SELECT [CALL_NAME], [SURNAME],**CALL_NAME + SURNAME AS [CALL_NAMESURNAME]** FROM [Sheet1$] where COY_NAME LIKE '%abc%'">
        </asp:SqlDataSource>

检查 ** ** 零件

<asp:DropDownList ID="DropDownList9" runat="server" 
            DataSourceID="SqlDataSource3" DataTextField="**CALL_NAMESURNAME**" 
            DataValueField="CALL_NAME">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:McCarthyConnectionString %>" 
            SelectCommand="SELECT [CALL_NAME], [SURNAME],**CALL_NAME + SURNAME AS [CALL_NAMESURNAME]** FROM [Sheet1$] where COY_NAME LIKE '%abc%'">
        </asp:SqlDataSource>

CHECK THE ** ** PARTS

最偏执的依靠 2024-11-21 21:14:53
IList<PM_ClientInformation> objPM_ClientInformation = new List<PM_ClientInformation>();

//get information in objPM_ClientInformation 

            foreach (PM_ClientInformation f in objPM_ClientInformation)
            {
                string text = string.Format("{0}{1}",
                    f.PM_cliFirstName.PadRight(10,'\u00A0'),
                    f.PM_cliLastName.PadRight(10,'\u00A0'));
                ddlClientName.Items.Add(new ListItem (text ,f.PM_cliId.ToString()));
            }

它绝对有效。

IList<PM_ClientInformation> objPM_ClientInformation = new List<PM_ClientInformation>();

//get information in objPM_ClientInformation 

            foreach (PM_ClientInformation f in objPM_ClientInformation)
            {
                string text = string.Format("{0}{1}",
                    f.PM_cliFirstName.PadRight(10,'\u00A0'),
                    f.PM_cliLastName.PadRight(10,'\u00A0'));
                ddlClientName.Items.Add(new ListItem (text ,f.PM_cliId.ToString()));
            }

it defiantly work.

喜爱纠缠 2024-11-21 21:14:53

下拉列表无法显示两个项目。你必须合并 trhem 然后将结果绑定到它。

请注意,还有一些其他下拉列表(基于 ajax 或简单)能够绑定到两个或更多列,但 ASP.net 无法处理它

Dropdownlist cannot display two items. you have to merge trhem and then bind the result to it.

note that there are some other dropdownlist (ajax based or simple ) which are able to bind to two or more columns, but ASP.net is not able to cope with it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文