技术开发 频道

ASP.NET 4.0新特性之Web.config的改进

  4、EnablePersistedSelection 属性的用法

  EnablePersistedSelection.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
    CodeBehind
="EnablePersistedSelection.aspx.cs" Inherits="AspDotNet.EnablePersistedSelection" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    
<!--
        EnablePersistedSelection
- 保存选中项的方式
            
true - 根据 DataKeyNames 指定的字段做为关键字保存选中项(分页操作不会改变选中项)
            
false - 根据行在当前页的表中的索引做为关键字保存选中项(分页后,选中项会发生改变。比如,在第一页选中了第一行,那么分页到第二页的时候选此页的第一行就会被当成选中项,也就是选中项发生了改变)
    
-->

    
<asp:GridView ID="gridView" runat="server" AllowPaging="True" DataSourceID="ObjectDataSource1"
        CellPadding
="4" ForeColor="#333333" GridLines="None" EnablePersistedSelection="true"
        DataKeyNames
="productId">
        
<AlternatingRowStyle BackColor="White" />
        
<Columns>
            
<asp:CommandField ShowSelectButton="True" />
            
<asp:BoundField DataField="productId" HeaderText="productId" SortExpression="productId" />
            
<asp:BoundField DataField="productName" HeaderText="productName" SortExpression="productName" />
            
<asp:BoundField DataField="productPrice" HeaderText="productPrice" SortExpression="productPrice" />
        
</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" />
        
<SortedAscendingCellStyle BackColor="#FDF5AC" />
        
<SortedAscendingHeaderStyle BackColor="#4D0000" />
        
<SortedDescendingCellStyle BackColor="#FCF6C0" />
        
<SortedDescendingHeaderStyle BackColor="#820000" />
    
</asp:GridView>
    
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetData"
        
TypeName="AspDotNet.EnablePersistedSelection"></asp:ObjectDataSource>

</asp:Content>

 

  EnablePersistedSelection.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AspDotNet
{
    
public partial class EnablePersistedSelection : System.Web.UI.Page
    {
        protected void Page_Load(
object sender, EventArgs e)
        {
            
        }

        
// 为 GridView 提供数据
        
public List<Product> GetData()
        {
            Random random
= new Random();

            List
<Product> products = new List<Product>();
            
for (int i = 0; i < 100; i++)
            {
                products.Add(
new Product { ProductId = i + 1, ProductName = "名称", ProductPrice = random.NextDouble() });
            }

            return products;
        }
    }

    
// 为 GridView 提供数据的实体类
    
public class Product
    {
        
public int ProductId { get; set; }
        
public string ProductName { get; set; }
        
public double ProductPrice { get; set; }
    }
}

  

0
相关文章