对于在学校的练习,我需要建立一个网站,在选择一个类型(在我的情况下是自行车)后从SQL数据库中获取行。我的大部分工作除了ListView之外,当我在下拉列表中选择一个选项时它不会更新。我认为这是一个SQL存储过程错误,但我似乎无法找到问题。
存储过程;
从db中获取需要显示在下拉列表中的类型(这可行)
CREATE PROCEDURE [dbo].[Procedure2]
AS
select a_type
from Artikelen
group by a_type
order by a_type
RETURN
这个是从下拉列表中获取具有用户所选类型的行。 @type应该是下拉列表中的选定值。
CREATE PROCEDURE [dbo].[OphaalFietsBijType]
@type varchar(50)
AS
select artikelnr, a_omschr, a_prijs
from Artikelen
where a_type = @type
RETURN
ASP.NET代码
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="A_type" DataValueField="A_type">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="Procedure2" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="artikelnr" DataSourceID="Ophalen">
<AlternatingItemTemplate>
<tr style="">
<td>
<asp:Label ID="artikelnrLabel" runat="server" Text='<%# Eval("artikelnr") %>' />
</td>
<td>
<asp:Label ID="a_omschrLabel" runat="server" Text='<%# Eval("a_omschr") %>' />
</td>
<td>
<asp:Label ID="a_prijsLabel" runat="server" Text='<%# Eval("a_prijs") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="artikelnrLabel1" runat="server" Text='<%# Eval("artikelnr") %>' />
</td>
<td>
<asp:TextBox ID="a_omschrTextBox" runat="server" Text='<%# Bind("a_omschr") %>' />
</td>
<td>
<asp:TextBox ID="a_prijsTextBox" runat="server" Text='<%# Bind("a_prijs") %>' />
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="artikelnrTextBox" runat="server" Text='<%# Bind("artikelnr") %>' />
</td>
<td>
<asp:TextBox ID="a_omschrTextBox" runat="server" Text='<%# Bind("a_omschr") %>' />
</td>
<td>
<asp:TextBox ID="a_prijsTextBox" runat="server" Text='<%# Bind("a_prijs") %>' />
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Label ID="artikelnrLabel" runat="server" Text='<%# Eval("artikelnr") %>' />
</td>
<td>
<asp:Label ID="a_omschrLabel" runat="server" Text='<%# Eval("a_omschr") %>' />
</td>
<td>
<asp:Label ID="a_prijsLabel" runat="server" Text='<%# Eval("a_prijs") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server">artikelnr</th>
<th runat="server">a_omschr</th>
<th runat="server">a_prijs</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="">
<td>
<asp:Label ID="artikelnrLabel" runat="server" Text='<%# Eval("artikelnr") %>' />
</td>
<td>
<asp:Label ID="a_omschrLabel" runat="server" Text='<%# Eval("a_omschr") %>' />
</td>
<td>
<asp:Label ID="a_prijsLabel" runat="server" Text='<%# Eval("a_prijs") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="Ophalen" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="OphaalFietsBijType" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:SessionParameter Name="type" SessionField="@type" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
答案 0 :(得分:0)
您使用的是SessionParameter
,但未在任何位置设置会话变量。您也不需要,因为您可以直接从DropDownList
获得价值,而无需任何复杂的逻辑。你所追求的是ControlParameter
。
只需将参数更改为以下内容:
<asp:ControlParameter ControlID="DropDownList1" PropertyName="SelectedValue" Name="type" Type="String" />