|
![]() |
---|
|
departmentList
True
departmentDS
。
courseGridView
。
departmentDS
EntityDataSource 控件,单击其智能标记并选择“配置数据源”命令。
departmentList
DropDownList 控件,单击智能标记,然后单击“选择数据源”。
![]() |
---|
|
schoolContext
属性。
departmentList
DropDownList 控件。
departmentList
控件的 SelectedIndexChanged 事件处理程序。
// Get the department ID. Int32 departmentID = Convert.ToInt32(departmentList.SelectedValue); // Select course information based on department ID. var courseInfo = from c in schoolContext.Courses where c.Department.DepartmentID == departmentID select new { CourseID = c.CourseID, CourseTitle = c.Title, CourseCredits = c.Credits }; // Bind the GridView control to the courseInfo collection. courseGridView.DataSource = courseInfo; courseGridView.DataBind();
<body> <form id="form1" runat="server"> <asp:DropDownList ID="departmentList" runat="server" AutoPostBack="True" DataSourceID="departmentDS" DataTextField="Name" DataValueField="DepartmentID" Height="19px" Width="161px"> </asp:DropDownList> <asp:EntityDataSource ID="departmentDS" runat="server" ConnectionString="name=SchoolEntities" DefaultContainerName="SchoolEntities" EnableFlattening="False" EntitySetName="Departments" Select="it.[DepartmentID], it.[Name]"> </asp:EntityDataSource> <asp:GridView ID="courseGridView" runat="server"> </asp:GridView> </form> </body>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.Objects; namespace CourseManagerWeb { public partial class _Default : System.Web.UI.Page { // Create an ObjectContext based on SchoolEntity. private SchoolEntities schoolContext; protected void Page_PreRenderComplete(object sender, EventArgs e) { // Force initial GridView update. departmentList_SelectedIndexChanged(this.Page, new EventArgs()); } protected void Page_Load(object sender, EventArgs e) { // Initialize the data context. schoolContext = new SchoolEntities(); } protected void departmentList_SelectedIndexChanged(object sender, EventArgs e) { // Get the department ID. Int32 departmentID = Convert.ToInt32(departmentList.SelectedValue); // Select course information based on department ID. var courseInfo = from c in schoolContext.Courses where c.Department.DepartmentID == departmentID select new { CourseID = c.CourseID, CourseTitle = c.Title, CourseCredits = c.Credits }; // Bind the GridView control to the courseInfo collection. courseGridView.DataSource = courseInfo; courseGridView.DataBind(); } } }
|
来自: 绚爷好人图书馆 > 《edmx 数据模型》