ASP.NET Binding DropDownList抛出错误

时间:2016-08-11 15:07:38

标签: asp.net data-binding drop-down-menu

我正在尝试绑定asp:DropDownList,但我一直收到错误

  

发生了'System.Web.HttpException'类型的异常   System.Web.dll但未在用户代码中处理

     

其他信息:DataBinding:'System.Data.DataRowView'可以   不包含名称为“id_enabled”的属性

这是我的SqlDataSource

<asp:SqlDataSource 
    ID="sql_enabled_ddl" 
    runat="server" 
    ConnectionString="<%$ ConnectionStrings:Tip-Tour %>"  

    SelectCommand ="SELECT 
                        id_enabled, 
                        description

                    FROM
                    (
                        SELECT 
                            1 AS id_enabled,
                            'true' AS description

                         UNION 

                         SELECT 
                            2 AS id_enabled,
                            'false' AS description

                    ) AS passport_enabled">

</asp:SqlDataSource>

和我的DropDownList

<asp:DropDownList
    ID="DropDownList2"
    runat="server"
    DataSourceID="sql_enabled_ddl"
    DataTextField="description"
    DataValueField="id_enabled"
    SelectedValue='<%# Bind("id_enabled") %>' 
    Width="87%">
</asp:DropDownList>     

1 个答案:

答案 0 :(得分:0)

您不需要绑定到 SelectedValue ,除非您使用像GridView这样的内部数据控件。

删除SelectedValue='<%# Bind("id_enabled") %>'

<asp:SqlDataSource
    ID="sql_enabled_ddl"
    runat="server"
    ConnectionString="<%$ ConnectionStrings:Tip-Tour %>"
    SelectCommand="SELECT 
                id_enabled, 
                description

            FROM
            (
                SELECT 
                    1 AS id_enabled,
                    'true' AS description

                    UNION 

                    SELECT 
                    2 AS id_enabled,
                    'false' AS description

            ) AS passport_enabled"></asp:SqlDataSource>
<asp:DropDownList
    ID="DropDownList2"
    runat="server"
    DataSourceID="sql_enabled_ddl"
    DataTextField="description"
    DataValueField="id_enabled"
    Width="87%">
</asp:DropDownList>