简体   繁体   English

如何显示组合了2个不同属性的dropdownlist datavaluefield?

[英]How to display dropdownlist datavaluefield with 2 different attributes combined?

I have a table in SQL : 我在SQL中有一个表:

WarehouseAddress(addressID, address1, address2, etc.)

How can i display the datavaluefield in my dropdownlist as a combination of address1 + address2 separated by a comma? 我怎样才能显示在下拉列表我的datavaluefield作为组合address1 + address2用逗号隔开?

This is my current aspx.cs code: 这是我当前的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();

I have tried using: 我试过使用:

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

But it just simply searches for the value "address1, address2" in sql, which simply does not exist in my WarehouseAddress table. 但是它只是简单地在sql中搜索值"address1, address2" ,而在我的WarehouseAddress表中根本不存在该值。

Thank you very much in advance for any kind of help! 预先非常感谢您提供的任何帮助!

you can combine that from your SQL statement and give it a column name address. 您可以将其与SQL语句合并,并为其指定列名称地址。 see below example of code rewritten from your code. 请参阅下面的代码示例,这些代码是从您的代码中重写的。

SQL Query: SQL查询:

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

Change in Source code for DataTextField: 更改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();

The answer to the question I asked earlier would be: 我之前问的问题的答案是:

aspx.cs: 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: SQL:

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

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

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