ASP.NET 4 Web Forms: Url Routing dan LinqDataSource

Pada posting ini saya akan menulis tentang bagaimana memanfaatkan Url Routing apabila kita menggunakan LinqDataSource.

Data yang saya pakai adalah Northwind database, project dibuat di Visual Studio 2010 dan template Web Application Project.

Setelah membuat project baru, saya tambahkan kedua entitas ini ke LingToSql Designer.

image

Dan kode berikut di Global.asax.cs

void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}
 
void RegisterRoutes(RouteCollection routes)
{
    // Category
    routes.MapPageRoute(
        "Category",
        "Category/{CategoryId}",
        "~/Category.aspx"
    );
}

Kemudian saya ganti kode di Default.aspx menjadi

<h2>
    Categories
</h2>
<ul>
    <asp:ListView ID="lvCategories" runat="server" DataSourceID="LinqDataSource1">
        <ItemTemplate>
            <li>
                <a href='<%# Page.GetRouteUrl("Category", new { CategoryId = Eval("CategoryId") }) %>'
                    runat="server">
                <%# Eval("CategoryName") %>
                </a> 
            </li>
        </ItemTemplate>
    </asp:ListView>
</ul>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
    ContextTypeName="ASPNET4WebFormsUrlRoutingLingDataSource.Models.NorthwindDataContext" 
    EntityTypeName="" OrderBy="CategoryName" 
    Select="new (CategoryName, CategoryID)" TableName="Categories">
</asp:LinqDataSource>

Dan menambahkan Category.aspx yang berisi kode berikut

<%@ Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Category.aspx.cs" Inherits="ASPNET4WebFormsUrlRoutingLingDataSource.Category" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
        Products by <%= CategoryName %>
    </h2>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="LinqDataSource1" AllowPaging="True" AllowSorting="True" >
        <Columns>
            <asp:BoundField DataField="ProductName" HeaderText="Product Name" 
                ReadOnly="True" SortExpression="ProductName" />
            <asp:BoundField DataField="QuantityPerUnit" HeaderText="Quantity Per Unit" 
                ReadOnly="True" SortExpression="QuantityPerUnit" />
            <asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" ReadOnly="True" 
                SortExpression="UnitPrice" DataFormatString="{0:C2}" />
        </Columns>
    </asp:GridView>
    <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
        ContextTypeName="ASPNET4WebFormsUrlRoutingLingDataSource.Models.NorthwindDataContext" 
        EntityTypeName="" OrderBy="ProductName" 
        Select="new (ProductName, QuantityPerUnit, UnitPrice)" 
        TableName="Products" Where="CategoryID == @CategoryID">
        <WhereParameters>
            <asp:RouteParameter DefaultValue="1" Name="CategoryID" RouteKey="CategoryId" 
                Type="Int32" />
        </WhereParameters>
    </asp:LinqDataSource>
</asp:Content>

Dan backend-nya

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASPNET4WebFormsUrlRoutingLingDataSource.Models;
 
namespace ASPNET4WebFormsUrlRoutingLingDataSource
{
    public partial class Category : System.Web.UI.Page
    {
        protected string CategoryName;
 
        protected void Page_Load(object sender, EventArgs e)
        {
            int categoryId = Convert.ToInt32(Page.RouteData.Values["CategoryId"]);
            NorthwindDataContext context = new NorthwindDataContext();
            var category = context.Categories
                .Where(c => c.CategoryID == categoryId)
                .SingleOrDefault();
 
            CategoryName = category.CategoryName;
            Page.Title = CategoryName;
        }
    }
}


Lalu apa yang menarik? Thanks to the new RouteParameter class, kita bisa dengan mudah menjadikan parameter routing sebagai WhereParameter dari LinqDataSource.

image

Kita juga bisa sedikit malas, karena kita bisa melakukannya dari konfigurasi LinqDataSource yang bisa kita akses dari SmartTag.

image

Dan berikut hasil akhirnya

image

image

Semoga bermanfaat.

Download Project Files

Share this post: | | | |
Published Friday, April 16, 2010 1:50 AM by Ferry Meidianto

Comments

# ASP.NET 4 Web Forms: RouteUrl dan RouteValue expression

Friday, April 16, 2010 2:37 AM by Ferry @ INDC

RouteUrl Pada 2 post sebelumnya: ASP.NET 4 Web Forms: Url Routing ASP.NET 4 Web Forms: Url Routing dan

Powered by Community Server (Commercial Edition), by Telligent Systems