ASP.NET 4.0 的新特性(2 在WebForm中使用Routing)(翻译)
接着上次的那个翻译,希望能对大家的学习有一点帮助!
原文:http://geekswithblogs.net/ranganh/archive/2009/08/20/whatrsquos-new-in-asp.net-4.0-ndash-part-ii-ndash-routing.aspx
在原文中有SourceCode下载
在asp.net 4.0 中 有新的System.Web.Routing,他提供所有的重要的类RouteTable,PageRouteHanlder类,在最初System.Web.Routing是整合在asp.net MVC 中的,可是asp.net 团队
eb需要发现这个组件很有用,就将他推广到WebForm中,所以我们只需要引用相应的DLL就能在WebFrom上使用啦。
他有什么用啦,他能帮助你创建友好的URL,协助搜索引擎优化。清楚URLs中不友好的编码,而使得搜索引擎能能高性能的检测URL.
现在就来做一个Sample.使用是VisualStudio和Northwind数据库。
首先创建一个asp.net web Application,再创建如下文件Products.aspx, Categories.aspx,Global.asax 当然还加上default.aspx
在default.aspx上配置一个GridView.
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="CategoryID" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="CategoryName" SortExpression="CategoryName">
<ItemTemplate>
<a href="Categories/<%# Eval("CategoryName") %>"><asp:Label ID="Label1" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
</asp:GridView>
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="CategoryID" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="CategoryName" SortExpression="CategoryName">
<ItemTemplate>
<a href="Categories/<%# Eval("CategoryName") %>"><asp:Label ID="Label1" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Description"
SortExpression="Description" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
</asp:GridView>
在上面的item Template上你能看到新修改的连接的情况,也就是说在URL上显示的Categories/Beverages,是“Categories” 页面上的CategoryName。
接着再在 Categories 页面上增加一个
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="CategoryName" SortExpression="CategoryName">
<ItemTemplate>
<a href="Products/<%# Eval("ProductName") %>"><asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit"
SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder"
SortExpression="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel"
SortExpression="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued"
SortExpression="Discontinued" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="CategoryName" SortExpression="CategoryName">
<ItemTemplate>
<a href="Products/<%# Eval("ProductName") %>"><asp:Label ID="Label1" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label></a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="QuantityPerUnit" HeaderText="QuantityPerUnit"
SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock"
SortExpression="UnitsInStock" />
<asp:BoundField DataField="UnitsOnOrder" HeaderText="UnitsOnOrder"
SortExpression="UnitsOnOrder" />
<asp:BoundField DataField="ReorderLevel" HeaderText="ReorderLevel"
SortExpression="ReorderLevel" />
<asp:CheckBoxField DataField="Discontinued" HeaderText="Discontinued"
SortExpression="Discontinued" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
然后配置好DataSource,可以看下面的图:
注意,我已算中Where条件下CategoryName下的Column=”Route“的Source。如图一,而且还描述RouteKey为”catname“和DefaultValue为"Beverages"。之后,我只是点击"Add"并点"OK"
(注意我们并没有写Ado。net代码,主要是为演示在DataSource上的Route新特性),这里Route类型是DataSource新增加的特性。
在DataSouce中的配置为:
Code
还要在Categories.aspx页面上填加一个Lable来显示过滤器并且值是通过Page.RouteData.Values["catname"] 获得;
protected void Page_Load(object sender, EventArgs e)
{
if (Page.RouteData.Values["catname"] != null)
{
lblDisplay.Text += "<b>" + Page.RouteData.Values["catname"].ToString() + "</b>";
}
else
{
lblDisplay.Visible = false;
}
}
{
if (Page.RouteData.Values["catname"] != null)
{
lblDisplay.Text += "<b>" + Page.RouteData.Values["catname"].ToString() + "</b>";
}
else
{
lblDisplay.Visible = false;
}
}
在配置Route前,我还要添加DetailView在Products.aspx页面上,在产品显示完成后。我们就在页面上 Global.asax文件中的Application_Start事件中添加Route控制。
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add("ProductRoute", new Route("Categories/Products/{productname}",
new PageRouteHandler("~/Products.aspx")));
RouteTable.Routes.Add("CategoryRoute", new Route("Categories/{catname}",
new PageRouteHandler("~/Categories.aspx")));
}
所以在,在Default.aspx页面上,你连接到/Categories/<CategoryName> ,这时是关于跳转在Categories.aspx 页面上。而到Products页面是连接{
RouteTable.Routes.Add("ProductRoute", new Route("Categories/Products/{productname}",
new PageRouteHandler("~/Products.aspx")));
RouteTable.Routes.Add("CategoryRoute", new Route("Categories/{catname}",
new PageRouteHandler("~/Categories.aspx")));
}
/Categories/Products/<ProductName>
所以你最后注意的就是URL的显示:
分别是http://localhost/Categories/Condiments 和 http://localhost/Categories/Products/Aniseed%20Syrup
www.cnblogs.com/worksguo
worksguo