Asp.net 将SqlDataSource替换为EntityDataSource

Asp.net 将SqlDataSource替换为EntityDataSource,asp.net,entity-framework,Asp.net,Entity Framework,我有一个带有countryID列的DataGrid(destination),它绑定到DropDownList以查看国家。我也有countries和countryGroups表,其中countries.countryGroupId=countryGroups.countryGroupId。列表必须格式化,因此我使用SqlDataSource获取格式化列表: SELECT c.countryID, cg.countryGroupName+': '+c.CountryName as Name F

我有一个带有countryID列的DataGrid(destination),它绑定到DropDownList以查看国家。我也有countries和countryGroups表,其中countries.countryGroupId=countryGroups.countryGroupId。列表必须格式化,因此我使用SqlDataSource获取格式化列表:

SELECT   c.countryID, cg.countryGroupName+': '+c.CountryName as Name
FROM     countries AS c INNER JOIN
         countries_groups AS cg ON c.countryGroupID = cg.countryGroupID
ORDER BY cg.Sort, c.CountryName
以下是最初绑定到sqldatasource的dropdownlist:

<asp:DropDownList ID="ddlCountryID" runat="server" Width="160px" DataSourceID="dsCountries"
     AppendDataBoundItems="true"  DataTextField="Name" DataValueField="countryID" 
     SelectedValue='<%# Bind("countryID") %>'>
     <asp:ListItem Value="" Text="Select"></asp:ListItem>
</asp:DropDownList>
而dropdownlist成为:

<asp:DropDownList ID="ddlCountryID" runat="server" Width="160px" AppendDataBoundItems="true"  
                  SelectedValue='<%# Bind("countryID") %>' **OnInit="ddlCountryID_OnInit"**  
                  DataTextField="Name" DataValueField="countryID">
                  <asp:ListItem Value="" Text="Select"></asp:ListItem>
</asp:DropDownList>

您可以使用此查询

       ProductionEntities db = new ProductionEntities();
       ddlCountryID.DataSource = (from c in db.countries 
                                  join cg in db.countries_groups on c.countryGroupID       
                                  equals cg.countryGroupID 
                                  select new 
                                  { c.countryID,   
                                    Name = cg.countryGroupName + " " + c.CountryName,cg.Sort})
                                   .OrderBy(w => new {w.countryID,w.sort });      
        ddlCountryID.DataTextField = "Name";
        ddlCountryID.DataValueField = "countryID";
        ddlCountryID.DataBind();

看起来一切都完成了(只是我在web.config中看不到连接字符串)。为了提高性能,我建议使用存储过程而不是Sql查询。是否必须使用web表单?为什么不将html选择与Knockjout.Js+WebApi一起使用呢?不,它根本没有完成。它错过了entitydatasource在国家和国家/地区组之间的连接。这是主要的不完整性。是的,必须使用webforms。我希望在服务器端保留业务逻辑,而不是javascript。好吧,您必须澄清您的问题。应该是“我应该如何改进我的数据访问层和sql查询空间”。Java Scriprt实验室(如Knockout)负责数据表示,业务逻辑始终位于服务器端,但现在有更好的解决方案——WebApi或MVC。关于您的问题-在实体数据源中使用存储过程。只需检查下面的答案。我刚刚更改了代码,这将解决您的问题。现在检查它。我应该将此代码放在哪里?例如,表单加载?在页面加载时绑定网格视图。然后将此代码放在这个.inside GridView1_RowDataBound事件中
<asp:DropDownList ID="ddlCountryID" runat="server" Width="160px" AppendDataBoundItems="true"  
                  SelectedValue='<%# Bind("countryID") %>' **OnInit="ddlCountryID_OnInit"**  
                  DataTextField="Name" DataValueField="countryID">
                  <asp:ListItem Value="" Text="Select"></asp:ListItem>
</asp:DropDownList>
       ProductionEntities db = new ProductionEntities();
       ddlCountryID.DataSource = (from c in db.countries 
                                  join cg in db.countries_groups on c.countryGroupID       
                                  equals cg.countryGroupID 
                                  select new 
                                  { c.countryID,   
                                    Name = cg.countryGroupName + " " + c.CountryName,cg.Sort})
                                   .OrderBy(w => new {w.countryID,w.sort });      
        ddlCountryID.DataTextField = "Name";
        ddlCountryID.DataValueField = "countryID";
        ddlCountryID.DataBind();