另一个下拉列表

时间:2015-06-23 15:19:05

标签: asp.net

我正在尝试创建两个下拉列表,第一个将选择一年,第二个将基于所选值(即ID),然后将仅显示具有yearID的表单,该表格等于所选的ID值。我认为我有正确的事情,因为它不起作用。

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Year" DataValueField="ID" AppendDataBoundItems="true" >
    <asp:ListItem>-- choose one --</asp:ListItem>
</asp:DropDownList><asp:DropDownList ID="DropDownList2" runat="server" AppendDataBoundItems="True" DataSourceID="SqlDataSource2" DataTextField="Form" DataValueField="ID">
    <asp:ListItem>-- choose one --</asp:ListItem>
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:SignInOutConnectionString %>" SelectCommand="SELECT [Form], [ID] FROM [PForm] WHERE ([YearID] = @YearID)">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" DbType="Int16" Name="YearID" PropertyName="SelectedValue" Type="Int16" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SignInOutConnectionString %>" SelectCommand="SELECT [Year], [ID] FROM [PYear]"></asp:SqlDataSource>`

然而,当我加载它时,它会说&#34;参数&#39; YearID&#39;的Type属性。设置DbType属性时无法设置。&#34;

1 个答案:

答案 0 :(得分:0)

我已经解决了我的问题,但会在这里为其他遇到此问题的人发布答案。

<asp:Label ID="Label1" runat="server">
    <asp:TextBox ID="TextFirstName" runat="server" placeholder="First Name" AutoCompleteType="Disabled" Height="55px" Width="300px" Font-Size="40px" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter First Name" ControlToValidate="TextFirstName"></asp:RequiredFieldValidator>
    <br />
    <br />
    <asp:TextBox ID="TextLastName" runat="server" placeholder="Last name" AutoCompleteType="Disabled" Height="55px" Width="300px" Font-Size="40px" />
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please Enter Last Name" ControlToValidate="TextLastName"></asp:RequiredFieldValidator>
    <br />

    <asp:ImageButton ID="Button1" runat="server" OnClick="Button1_Click" ImageUrl="~/Images/Next.png" ImageAlign="Right" />
</asp:Label><asp:Label ID="Label2" runat="server">
    <asp:TextBox ID="TextReason" runat="server" placeholder="Reason" AutoCompleteType="Disabled" Height="55px" Width="300px" Font-Size="40px" />
    <asp:ImageButton ID="Button2" runat="server" ImageUrl="~/Images/Next.png" OnClick="Button2_Click" />
</asp:Label><asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Year" DataValueField="ID" AppendDataBoundItems="true">
    <asp:ListItem Value="0">-- Choose One --</asp:ListItem>
</asp:DropDownList><asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="Form" DataValueField="ID" AppendDataBoundItems="true">
    <asp:ListItem Value="0">-- Choose One --</asp:ListItem>
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:SignInOutConnectionString %>" SelectCommand="SELECT [Form], [ID] FROM [PForm] WHERE ([YearID] = @YearID)">
    <SelectParameters>
        <asp:ControlParameter ControlID="DropDownList1" Name="YearID" PropertyName="SelectedValue" Type="int16" />
    </SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SignInOutConnectionString %>" SelectCommand="SELECT [Year], [ID] FROM [PYear]"></asp:SqlDataSource>

我更改的是我从控制参数中删除了DbType =“Int”。但是在改变了一些其他的东西后,我意识到我的ListItem导致错误,它不会转换为int,因为值是一个nvarchar,所以我不得不将ListItem的值设置为0.这很好,希望这可以帮助任何有我问题的人。 :)