Oracle 5.0 Reference Manual page 1909

Table of Contents

Advertisement

Populating a Drop Data List Box with using the results of a entity LINQ query
In this part of the tutorial you will write code to populate the DropDownList control. When the web page
loads the data to populate the list will be achieved by using the results of a LINQ query on the model
created previously.
1. In the Design view panel, double-click any blank area. This brings up the
2. Modify the relevant section of code according to the following listing:
...
public partial class _Default : System.Web.UI.Page
{
worldModel.worldEntities we;
protected void Page_Load(object sender, EventArgs e)
{
we = new worldModel.worldEntities();
if (!IsPostBack)
{
var countryQuery = from c in we.country
DropDownList1.DataValueField = "Code";
DropDownList1.DataTextField = "Name";
DropDownList1.DataSource = countryQuery;
DataBind();
}
}
...
Note that the list control only needs to be populated when the page first loads. The conditional code
ensures that if the page is subsequently reloaded, the list control is not repopulated, which would
cause the user selection to be lost.
3. Save the solution, build it and run it. You should see the list control has been populated. You can
select an item, but as yet the grid view control does not appear.
At this point you have a working Drop Down List control, populated by a LINQ query on your entity data
model.
Populating a Grid View control using an entity LINQ query
In the last part of this tutorial you will populate the Grid View Control using a LINQ query on your entity
data model.
1. In the Design view, double-click the DropDownList control. This causes its
SelectedIndexChanged
in the list control and thus fires an AutoPostBack event.
2. Modify the relevant section of code accordingly to the following listing:
...
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
var cityQuery = from c in we.city
GridView1.DataSource = cityQuery;
DataBind();
}
...
The grid view control is populated from the result of the LINQ query on the entity data model.
3. As a check compare your code to that shown in the following screenshot:
Connector/Net Tutorials
orderby c.Name
select new { c.Code, c.Name };
code to be displayed. This method is called when a user selects an item
where c.CountryCode == DropDownList1.SelectedValue
orderby c.Name
select new { c.Name, c.Population, c.CountryCode };
1889
method.
Page_Load

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents