WhereParameter不适用于下拉列表

时间:2013-12-28 08:25:38

标签: asp.net

我的表结构

Courses

  • Id
  • Description

Subjects

  • Id
  • Description
  • Courses(与课程表的关系)。

我的代码如下

  td>
   <asp:DropDownList ID="ddlCourses" runat="server" DataSourceID="LinqCourses"
       DataTextField="Description" DataValueField="Id"/>
   <asp:LinqDataSource ID="LinqCourses" runat="server"
       ContextTypeName= "DataAccess.SchoolStudyDataContext"  Select="new (Description, Id)"
       TableName="Courses" />
</td>
 <td align="left" valign="top" class="style5" style="width: 124px">
 <asp:DropDownList ID="ddlSubjects" runat="server" DataSourceID="LinqSubjects" 
      DataTextField="Description" DataValueField="Id"/>
      <asp:LinqDataSource ID="LinqSubjects" runat="server" 
     ContextTypeName= "DataAccess.SchoolStudyDataContext"  Select="new (Description,Courses,Id)"
     TableName="Subjects" Where="Courses == @Id" />
      <WhereParameters>
           asp:ControlParameter ControlID="ddlCourses" Name="Id"
           PropertyName="SelectedValue"  Type="Int32" />
     </WherePrameters>

2 个答案:

答案 0 :(得分:0)

<asp:DropDownList ID="ddlCourses" runat="server" DataSourceID="SqlDataSource1"
   DataTextField="Description" DataValueField="Id"/> 

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>" 

    SelectCommand="SELECT [Description],[Courses],[Id] FROM [table_name] WHERE ([Courses ] = @Courses ) ">
    <SelectParameters>

        <asp:Parameter  Name="Courses" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

这不是一个确切的答案,但这可能有助于我猜测

答案 1 :(得分:0)

您的Where Property是错误的。改变这个(==):

  Where="Courses == @Id" />

到此(=):

 Where="Courses = @Id" />

来源:MSDN