Sunday, January 25, 2009

A Short Example for LinqDataSource Control

Suppose that you want to fill a DropDownList with the list of products using a LinqDataSource control and then select a product with minimum unit price as the default value for the DropDownList.

1. Add a LINQ to SQL Class to your ASP.NET project (default name might be DataClasses.dbml).


2. Add tables that you need for your project (including the table for products that might be called Products as in the Microsoft Northwind database).


3. Declare a LinqDataSource control:

LinqDataSource lds = new LinqDataSource();


4. Set its ContextTypeName and TableName:


lds.ContextTypeName = "NWDataClassesDataContext";
lds.TableName = "Products";

The LinqDataSource control is ready be used!


5. Set data properties for your DropDownList:


DropDownList1.DataSource = lds;
DropDownList1.DataValueField = "ProductID";
DropDownList1.DataTextField ="ProductName";
DropDownList1.DataBind();


Now, your DropDownList is working.


6. Select a product with the minimum unit price as the default value selected in the DropDownList control:


DataClassesDataContext dc = new DataClassesDataContext();
decimal? minUnitPrice = (decimal?)(from p in dc.Products select p.UnitPrice).Min().Value;
var q = from p in dc.Products where p.UnitPrice == minUnitPrice select p.ProductID;
DropDownList1.SelectedValue = q.Min().ToString();


Run your project and see the result!

Saturday, January 24, 2009

Nested Master Page [LEVEL:: EASY]

“Nested master pages are supported since ASP.NET 2.0. The catch is that Visual Studio 2005 didn't support the design of nested masters very well. Thankfully, this capability changes radically with Visual Studio 2008, where nested master pages are fully supported in the page designer.” Reference: Programming Microsoft ASP.NET 3.5
by Dino Esposito MS Press

There are techniques for master pages to modify/improve their behavior based on the requirements of an ASP.NET project. This post shows how to nest one master page inside another.

In the following picture, the first line is written in a master page that is used as the top level master page, the second line is written in a nested master page (level 2) and the third line and the title of the page are written in an aspx file that uses our nested master page as it master page:

The source in the top level master page called MP_Level1:



The source in the nested master page called MP_Level2_No1:


And this is the source of the aspx file:


Friday, January 23, 2009

Entity Framework

Entity Framework

Watch MS' webcast here or at Microsoft!

Books for Entity Framework:

Entity Framework Tutorial

Programming Entity Framework

And MS' Lab for ADO.NET Entity Framework: click here


URL Routing in ASP.NET 3.5

URL Routing in ASP.NET 3.5


Service Pack 1 of .NET 3.5 introduced a routing engine to ASP.NET runtime (included in assembly System.Web.Routing.dll – see it in GAC folder). By this engine, URLs can map to no files of the website! In URL routing, some patterns may be defined which contain placeholders for parameters and values required to handle URL requests. See the following reference for more info and a code sample:

http://msdn.microsoft.com/en-us/magazine/2009.01.extremeaspnet.aspx

ASP.NET 3.5: EntityDataSource Control

ASP.NET 3.5: EntityDataSource Control

Service Pack 1 of .NET 3.5 introduced ADO.NET EntityDataSource control which can be employed in web applications that use the ADO.NET Entity Framework. EntityDataSource control provides data binding based on Entity Data Model (EDM). EDM presents data as sets of entities and the relationships among entities. EDM is used by Entity Framework in object-relational mapping (ORM or O/RM or O/R mapping) and in other technologies such as ADO.NET Data Services. EntityDataSource control manages create, read, update and delete operations on ASPX pages. It may be employed by all known controls with data binding such as ListView, DropDownList, etc. EntityDataSource can also get values for query parameter from controls, query strings, cookies and other parameter objects. For more information and how to employ this control, you may refer to the following books:


Programming Entity Framework


Professional ADO.NET 3.5 with LINQ and the Entity Framework


Entity Framework Tutorial


ASP.NET 3.5: LinqDataSource Class

ASP.NET 3.5: LinqDataSource Class