我正在尝试创建音乐商店应用程序。在服务(实际上是音乐商店)页面上,我创建了一个包含不同音乐流派的下拉列表。我已设法将下拉列表绑定到我的数据库类型。但是当我从下拉列表中选择特定选项时(例如Pop ...),我无法显示数据库中的音乐。
这就是我的代码对services.aspx的看法。
的流派
<hr class="hrsmallleft wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="600ms" />
</div>
<div><strong>POP SONGS</strong></div>
<div>
<asp:DropDownList id="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Genre" DataValueField="Name" OnSelectedIndexChanged="DropDown1_SelectedIndexChanged" AutoPostBack="True">
<asp:ListItem>--Select a Genere--</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:RegisterConnectionString %>" SelectCommand="SELECT * FROM [MusicRecord]"></asp:SqlDataSource>
</div>
<br />
<br />
<div class="col-md-4">
<div class="panel panel-success wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="600ms">
<div class="panel-heading">
<h4>
<i class="fa fa-fw fa-check"></i>Katy Perry - Prism</h4>
</div>
<div class="panel-body">
<p>
<img src="img/katpop.jpg" height="250" width="294"/>
</p>
<br />
<asp:HyperLink ID="HyperLink1" runat="server" CssClass="btn btn-success">Learn More</asp:HyperLink>
</div>
</div>
</div>
<!-- 1st content -->
<div class="col-md-4">
<div class="panel panel-success wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="600ms">
<div class="panel-heading">
<h4>
<i class="fa fa-fw fa-check"></i>Katy Perry - Prism</h4>
</div>
<div class="panel-body">
<p>
<img src="img/katpop.jpg" height="250" width="294"/>
</p>
<br />
<asp:HyperLink ID="HyperLink3" runat="server" CssClass="btn btn-success">Learn More</asp:HyperLink>
</div>
</div>
</div>
<div class="col-md-4">
<div class="panel panel-success wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="600ms">
<div class="panel-heading">
<h4>
<i class="fa fa-fw fa-check"></i>Katy Perry - Prism</h4>
</div>
<div class="panel-body">
<p>
<img src="img/katpop.jpg" height="250" width="294"/>
</p>
<br />
<asp:HyperLink ID="HyperLink2" runat="server" CssClass="btn btn-success">Learn More</asp:HyperLink>
</div>
</div>
</div>
<div class="col-md-4">
<div class="panel panel-success wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="600ms">
<div class="panel-heading">
<h4>
<i class="fa fa-fw fa-check"></i>Katy Perry - Prism</h4>
</div>
<div class="panel-body">
<p>
<img src="img/katpop.jpg" height="250" width="294"/>
</p>
<br />
<asp:HyperLink ID="HyperLink4" runat="server" CssClass="btn btn-success">Learn More</asp:HyperLink>
</div>
</div>
</div>
<div class="col-md-4">
<div class="panel panel-success wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="600ms">
<div class="panel-heading">
<h4>
<i class="fa fa-fw fa-check"></i>Katy Perry - Prism</h4>
</div>
<div class="panel-body">
<p>
<img src="img/katpop.jpg" height="250" width="294"/>
</p>
<br />
<asp:HyperLink ID="HyperLink5" runat="server" CssClass="btn btn-success">Learn More</asp:HyperLink>
</div>
</div>
</div>
<div class="col-md-4">
<div class="panel panel-success wow fadeInDown" data-wow-duration="1000ms" data-wow-delay="600ms">
<div class="panel-heading">
<h4>
<i class="fa fa-fw fa-check"></i>Katy Perry - Prism</h4>
</div>
<div class="panel-body">
<p>
<img src="img/katpop.jpg" height="250" width="294"/>
</p>
<br />
<asp:HyperLink ID="HyperLink6" runat="server" CssClass="btn btn-success">Learn More</asp:HyperLink>
</div>
</div>
</div>
</div>
</div>
</div>
答案 0 :(得分:0)
将选定的值属性添加到下拉列表
<asp:DropDownList id="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Genre" DataValueField="Name" OnSelectedIndexChanged="DropDown1_SelectedIndexChanged" AutoPostBack="True" SelectedValue='<%# Eval("Name") %>'>
<asp:ListItem>--Select a Genere--</asp:ListItem>
答案 1 :(得分:0)
为什么不摆脱SQL数据源控件。将实体框架添加到您的项目中。运行LINQ查询并将结果绑定到DropdownList。然后“OnSelectedIndexChanged”事件运行另一个LINQ查询以从数据库中提取数据。
using (var rep = new efEntities()){
var results= rep.Genre.ToList();
// bind
DropDownList1.datasource=results;
DropDownList1.DataTextField="";
DropDownList1.DataValueField="";
DropDownList1.DataBind();
}
protected void DropDownList1_selectedindexChanged(){
int genreID = convert.toint32(DropDownList1.selectedvalue);
// search database
using (var rep = new efEntities()){
var results = rep.Music.Where(m=>m.genreID==genreID).ToList();
// loop to get results
foreach(var item in results){
//do stuff
}
}
}