如何显示下拉列表datavaluefield,结合了两个不同的属性?

时间:2017-05-17 07:35:29

标签: c# asp.net sql-server

我在 SQL

中有一个表格

WarehouseAddress(addressID, address1, address2, etc.)

如何将我的下拉列表中的datavaluefield显示为以逗号分隔的address1 + address2的组合?

这是我当前的 aspx.cs 代码:

//reading the data from SQL
    deliveryAddressDdl.DataSource = warehouseAddressDB.getShippingAddresses(uid, wid);

//setting the data to be displayed in the dropdownlist
    deliveryAddressDdl.DataTextField = "address1";

//setting the value inside the dropdownlist when selected
    deliveryAddressDdl.DataValueField = "addressID";
    deliveryAddressDdl.DataBind();

我尝试过使用:

deliveryAddressDdl.DataTextField = "address1" + ", " + "address2";

但它只是在sql中搜索值"address1, address2",这在我的WarehouseAddress表中根本不存在。

非常感谢您提供任何帮助!

2 个答案:

答案 0 :(得分:0)

您可以将其与SQL语句结合使用,并为其指定列名称地址。请参阅下面的代码重写代码示例。

SQL查询:

SELECT addressID, address1 + ' , ' + address2 AS Address FROM WarehouseAddress

DataTextField的源代码更改:

 //reading the data from SQL
 deliveryAddressDdl.DataSource = warehouseAddressDB.getShippingAddresses(uid, wid);

//setting the data to be displayed in the dropdownlist
deliveryAddressDdl.DataTextField = "Address";

//setting the value inside the dropdownlist when selected
deliveryAddressDdl.DataValueField = "addressID";
deliveryAddressDdl.DataBind();

答案 1 :(得分:0)

我之前问过的问题的答案是:

<强> aspx.cs:

//reading the data from SQL
    deliveryAddressDdl.DataSource = warehouseAddressDB.getShippingAddresses(uid, wid);

//setting the data to be displayed in the dropdownlist
    deliveryAddressDdl.DataTextField = "combinedAddress";

//setting the value inside the dropdownlist when selected
    deliveryAddressDdl.DataValueField = "addressID";
    deliveryAddressDdl.DataBind();

<强> SQL:

select *, (address1 + ',' + SPACE(1) + address2) as combinedAddress from WarehouseAddress