ASP.NET 4.0 的新特性(2 在WebForm中使用Routing)(翻译)
生活随笔
收集整理的這篇文章主要介紹了
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>
??? 在上面的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>
???? 然后配置好DataSource,可以看下面的圖:
注意,我已算中Where條件下CategoryName下的Column=”Route“的Source。如圖一,而且還描述RouteKey為”catname“和DefaultValue為"Beverages"。之后,我只是點擊"Add"并點"OK"
(注意我們并沒有寫Ado。net代碼,主要是為演示在DataSource上的Route新特性),這里Route類型是DataSource新增加的特性。
在DataSouce中的配置為:
Code
<asp:SqlDataSource?ID="SqlDataSource1"?runat="server"
???????????ConnectionString="<%$?ConnectionStrings:NorthwindConnectionString?%>"
???????????SelectCommand="SELECT?[ProductName],?[QuantityPerUnit],?[UnitPrice],?[UnitsInStock],?[UnitsOnOrder],?[ReorderLevel],?[Discontinued]?FROM?[Alphabetical?list?of?products]?WHERE?([CategoryName]?LIKE?'%'?+?@CategoryName?+?'%')">
???????????<SelectParameters>
???????????????<asp:RouteParameter?DefaultValue="Beverages"?Name="CategoryName"
???????????????????RouteKey="catname"?Type="String"?/>
???????????</SelectParameters>
???????</asp:SqlDataSource>
? 還要在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;
???????????}
???????}
?? 在配置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頁面是連接
/Categories/Products/<ProductName>
所以你最后注意的就是URL的顯示:
分別是http://localhost/Categories/Condiments 和 http://localhost/Categories/Products/Aniseed%20Syrup
?www.cnblogs.com/worksguo
worksguo
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的ASP.NET 4.0 的新特性(2 在WebForm中使用Routing)(翻译)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Redis复制
- 下一篇: 调整显示屏幕偏移和刷新频率[zt]