2016-08-11 68 views
1

我试图绑定一个asp:DropDownList但我一直收到错误ASP.NET绑定的DropDownList抛出错误

类型“System.Web.HttpException”的异常出现在 System.Web.dll中,但在用户代码中没有处理

其他信息:数据绑定:“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>  

回答

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> 
+0

tthat引发不同的错误....异常详细信息:System.Data.SqlClient.SqlException:无效的列名称'id_enabled'。 – blitzeus

+0

你能在测试新的aspx页面上面的代码中没有母版页? – Win

+0

这是一个FormView EditItemTemplate中 – blitzeus