下拉列表中的错误数据绑定

时间:2013-06-04 01:49:17

标签: asp.net vb.net data-binding string-formatting

我在我的aspx页面上有Dropdwon:

    <td>
    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextFormatString = "{0} - {1}" DataTextField = "value1,value2" DataValueField="Value2" class="ctrDropDown" onBlur="this.className='ctrDropDown';" onMouseDown="this.className='ctrDropDownClick';" onChange="this.className='ctrDropDown';">
     </asp:DropDownList>
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                                        ConnectionString="<%$ ConnectionStrings:XXXX_DevConnectionString_ddl %>" 
                                        SelectCommand="SELECT ParamId, ParamType, Value1, Value2, Value3, Status FROM Parameter WHERE (ParamType = 'BankId')">
 </asp:SqlDataSource>
</td>

我想让数据文本字段显示在value1 - value2,我做DataTextFormatString = "{0} - {1}" DataTextField = "value1,value2"但是它给了我一个错误:

Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'value1,value2'.

我的代码错了吗?还是我错过了什么?

1 个答案:

答案 0 :(得分:1)

解决方案相当简单:在SQL中执行计算(换成新列名),然后使用新列名作为DataTextField。

例如,添加一个新的结果字段:

SelectCommand="SELECT ParamId, ParamType, Value1, Value2, Value3, Status, Value1 - Value2 As Result FROM Parameter WHERE (ParamType = 'BankId')">

然后在DataTextField中引用它:

DataTextField = "result"