我尝试使用下拉列表更改我的DataSource使用的Select Command。页面加载时,根据所选索引设置select命令。当我更改下拉列表时,页面刷新但数据没有!我哪里错了?
.aspx文件
<asp:DropDownList ID="filmFilter" runat="server" OnSelectedIndexChanged="filmFilter_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem Value="">Filter</asp:ListItem>
<asp:ListItem Value="priceASC">Price: Low-High</asp:ListItem>
<asp:ListItem Value="priceDSC">Price: High-Low</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringFilms %>"></asp:SqlDataSource>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="filmID" DataSourceID="SqlDataSource1" OnSelectedIndexChanged="ListView1_SelectedIndexChanged">
<ItemTemplate>
...
</ItemTemplate>
<LayoutTemplate>
...
</LayoutTemplate>
</asp:ListView>
代码隐藏 - 页面加载:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (filmFilter.SelectedIndex == 0)
{
SqlDataSource1.SelectCommand = "SELECT * FROM [films]";
}
if (filmFilter.SelectedIndex == 1)
{
SqlDataSource1.SelectCommand = "SELECT * FROM [films] ORDER BY [filmPrice]";
}
if (filmFilter.SelectedIndex == 2)
{
SqlDataSource1.SelectCommand = "SELECT * FROM [films] ORDER BY [filmPrice] DESC";
}
}
}
谢谢!
答案 0 :(得分:2)
你已经得到了所以你的数据源是在page_load上设置的,这很好,但是你把它包装在
中if (!IsPostBack)
{
这意味着更改数据源的代码只会在您没有回发时发生(当您更改下拉列表的值时,您将在回发中)
请仔细阅读 http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx 它将有助于您的理解