Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将下拉菜单链接到包含产品的数据库,并在ASP中显示这些产品?_C#_Asp.net_Visual Studio - Fatal编程技术网

C# 如何将下拉菜单链接到包含产品的数据库,并在ASP中显示这些产品?

C# 如何将下拉菜单链接到包含产品的数据库,并在ASP中显示这些产品?,c#,asp.net,visual-studio,C#,Asp.net,Visual Studio,我正在尝试创建一个音乐商店应用程序。在音乐商店的服务页面上,我创建了一个包含不同音乐类型的下拉列表。我已经设法将下拉列表绑定到我的数据库类型。但是当我从下拉列表中选择一个特定选项时,我无法显示数据库中的音乐,例如Pop 这就是我的services.aspx代码的样子 流派 将选定值特性添加到下拉列表中 <asp:DropDownList id="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextFie

我正在尝试创建一个音乐商店应用程序。在音乐商店的服务页面上,我创建了一个包含不同音乐类型的下拉列表。我已经设法将下拉列表绑定到我的数据库类型。但是当我从下拉列表中选择一个特定选项时,我无法显示数据库中的音乐,例如Pop

这就是我的services.aspx代码的样子

流派


将选定值特性添加到下拉列表中

 <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>

为什么不摆脱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
}
}
}

将selectedvalue添加到下拉列表只会导致生成错误,因此请尝试在页面上的代码隐藏中添加所选值,如DropDownList1。selectedvalue=DropDownList1.Items.FindByValueName.Tostring;提供此严重性代码说明项目文件行错误CS1061'ListItem'不包含'Tostring'的定义,并且找不到接受'ListItem'类型第一个参数的扩展方法'Tostring'。是否缺少using指令或程序集引用?modern-business2 C:\Users\mahmoud\Documents\Software EngineeringUWE\Visual Studio文件\Visual Studio项目\Spotify-Music-Store-16oct1156\Spotify Music Store\modern business\services.aspx.cs 19[
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
}
}
}