In this example I will create simple web project that will have drop down list and grid view.
User will be able to select some value from drop down list and based on that selection grid view will be populated.
Step by step instructions can be found on this video:
1. Create Empty Web Application
2. Add entity to it (step by step instructions can be found in video or here http://howtodomssqlcsharpexcelaccess.blogspot.ca/2012/03/visual-studio-2010-ms-sql-2012-aspnet.html
3. Add web form then add to it drop down list and gridview
4. In code behind of the page add the following using statements
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Web.ModelBinding;
5. Create the following 2 methods that will load data for ddl and gridview
private SchoolEntities db = new SchoolEntities();
public IQueryable<Dpt> GetDepartments()
{
var query = this.db.Dpts;
return query;
}
public IQueryable<User> GetSelectedUser([Control("DropDownList1")] int? DepartmentId)
{
var query = this.db.Users
.Where(u => u.DepartmentId == DepartmentId);
return query;
}
protected void Page_Load(object sender, EventArgs e)
{
}
Note that GetSelectedUser method receives value from drop down list control
6. Now ensure that both controls have all needed properties:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" SelectMethod="GetDepartments"
ItemType="Model_Binding.Dpt" DataTextField="DepartmentName" DataValueField="DepartmentID" >
</asp:DropDownList>
<br />
<asp:GridView ID="GridView1" runat="server" ItemType="Model_Binding.User" SelectMethod="GetSelectedUser"
DataKeyNames="UserId" AllowPaging="True" AllowSorting="True" PageSize="10" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="First Name" SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" /> </Columns>
</asp:GridView>
7. Video example will show you database structure.
No comments:
Post a Comment