日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET 4.0 的新特性(2 在WebForm中使用Routing)(翻译)

發(fā)布時(shí)間:2025/3/21 asp.net 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET 4.0 的新特性(2 在WebForm中使用Routing)(翻译) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

??? 接著上次的那個(gè)翻譯,希望能對(duì)大家的學(xué)習(xí)有一點(diǎn)幫助!

原文: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 團(tuán)隊(duì)
eb需要發(fā)現(xiàn)這個(gè)組件很有用,就將他推廣到WebForm中,所以我們只需要引用相應(yīng)的DLL就能在WebFrom上使用啦。
他有什么用啦,他能幫助你創(chuàng)建友好的URL,協(xié)助搜索引擎優(yōu)化。清楚URLs中不友好的編碼,而使得搜索引擎能能高性能的檢測(cè)URL.
現(xiàn)在就來做一個(gè)Sample.使用是VisualStudio和Northwind數(shù)據(jù)庫。
首先創(chuàng)建一個(gè)asp.net web Application,再創(chuàng)建如下文件Products.aspx, Categories.aspx,Global.asax? 當(dāng)然還加上default.aspx
在default.aspx上配置一個(gè)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 頁面上增加一個(gè)

<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"。之后,我只是點(diǎn)擊"Add"并點(diǎn)"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頁面上填加一個(gè)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頁面上,在產(chǎn)品顯示完成后。我們就在頁面上 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> ,這時(shí)是關(guān)于跳轉(zhuǎn)在Categories.aspx 頁面上。而到Products頁面是連接

/Categories/Products/<ProductName>

所以你最后注意的就是URL的顯示:

分別是http://localhost/Categories/Condiments 和 http://localhost/Categories/Products/Aniseed%20Syrup




?www.cnblogs.com/worksguo
worksguo
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的ASP.NET 4.0 的新特性(2 在WebForm中使用Routing)(翻译)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。