超完美組合:LinqDataSource + ListView + DataPager + jQuery
网站的前台套版,使用
例如以下的例子就是透过一个 LinqDataSource 直接套出两层的 ListView,真的很漂亮。
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="MyDataContext"
TableName="ProductCategory"
Select="new (ID,Name,Image,ProductCategory_Child)">
</asp:LinqDataSource>
<asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource1" ItemPlaceholderID="item">
<LayoutTemplate>
<ul>
<li runat="server" id="item"></li>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<a href="ProductCatetory.aspx?id=<%#Eval("ID")%>"><%#Eval("Name")%></a>
<asp:ListView ID="ListView2" runat="server"
DataSource='<%# Eval("ProductCategory_Child") %>'
ItemPlaceholderID="item">
<LayoutTemplate>
<ul>
<li runat="server" id="item"></li>
</ul>
</LayoutTemplate>
<ItemTemplate>
<li><a href="Product.aspx?id=<%#Eval("ID")%>"><%#Eval("ProductName")%></a>
</ItemTemplate>
</asp:ListView>
</li>
</ItemTemplate>
</asp:ListView>
但是要能透用这种方式套版有两点要注意:
1. 必须先在 DBML ( Linq to SQL Class ) 中设定关连( Association ) 才可以使用。
2. 在 LinqDataSource 中必须明确的宣告 Select 属性中的关连字段名称。
而针对一些页面上的 UI 互动或细节的调校可以用 jQuery 来处理,学会 jQuery 可以节省你不少时间。
例如说两层子选单的第二层选单显示与否,就可以透过 jQuery 来设定是否展开,完全不需要在 ASP.NET 中写的死去活来的。
$(function()
{
$('ul.item_model a').each(function(i)
{
if(this.href.indexOf('<%=Request.QueryString["id"]%>') > 0)
{
$(this).addClass("active");
if($(this).parent().parent().attr('class') == 'item_model_list')
{
$(this).parent().parent().show();
}
if($(this).next().attr('class') == 'item_model_list')
{
$(this).next().show();
}
}
});
});
如果要用分页的话,就使用 ListView + DataPager 来处理,整个网页套版的动作十分的直觉。
相关连结
The asp:ListView control (Part 1 - Building a Product Listing Page with Clean CSS UI)
http://weblogs.asp.net/scottgu/archive/2007/08/10/the-asp-listview-control-part-1-building-a-product-listing-page-with-clean-css-ui.aspx
Using ASP.NET 3.5's ListView and DataPager Controls: Sorting Data with the ListView Control
http://aspnet.4guysfromrolla.com/articles/011608-1.aspx
ListView and DataPager in ASP.NET 3.5
http://www.west-wind.com/WebLog/posts/127340.aspx
Paging Through Data with the ASP.NET 3.5 ListView and DataPager Controls
http://aspnet.4guysfromrolla.com/articles/021308-1.aspx
Building a Grouping Grid with the ASP.NET 3.5 LinqDataSource and ListView Controls ( 推荐啦 )
http://mattberseth.com/blog/2008/01/building_a_grouping_grid_with.html
Some ASP.NET 3.5 ListView Control Examples
http://blogs.msdn.com/mikeormond/archive/2008/01/23/some-asp-net-3-5-listview-control-examples.aspx
ASP.NET 3.5 的 ListView 控件与 CSS Friendly
http://www.cnblogs.com/cathsfz/archive/2007/08/22/864797.html
jQuery 官方网站
http://jquery.com/
jQuery 教学 (中文的喔)
http://jsgears.com/thread-63-1-1.html
此文章由 will 發表於 2008/3/17 上午 12:01:00
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述