首页 > 其他 > 详细

ListView与ObjectDataSource实现CRUD及分页排序细节

时间:2014-03-24 17:19:47      阅读:458      评论:0      收藏:0      [点我收藏+]

#ListView绑定数据,选择ObjectDataSource作为数据源

ObjectDataSource中设置 TypeName="NorthwindBusiness"设置业务层数据获取类,然后设置数据查询方法SelectMethod="GetData"与 计算数据总行数的方法SelectCountMethod="GetCount",设置允许分页EnablePaging="True" 以及传递给SelectMethod方法的参数每页显示数据行数MaximumRowsParameterName="pageSize" 和每页开始行索引号StartRowIndexParameterName="startIndex",前端页代码如下:

bubuko.com,布布扣
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="NorthwindBusiness"
        SelectMethod="GetData" EnablePaging="True" 
        SelectCountMethod="GetCount"
        MaximumRowsParameterName="pageSize" 
        StartRowIndexParameterName="startIndex">
</asp:ObjectDataSource>
bubuko.com,布布扣

其中业务层NorthwindBusiness类中GetData代码示例如下:

bubuko.com,布布扣
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace BLL
{
    public class NorthwindBusiness
    {
        DbHelperSQL helper = new DbHelperSQL(ConfigurationManager.ConnectionStrings["NorthWind"].ToString());
        int DataCount = 0;
        public DataSet GetData(int pageSize,int startIndex,string sortE)
        {
            string sql_condition = " order by " + sortE;
            string strsql = "select * from Customers";
            string countsql = "select count(1) from Customers";
            DataCount = helper.GetCount(countsql);
            DataSet ds = new DataSet();
            strsql += sql_condition;
            using(SqlConnection conn= new SqlConnection(ConfigurationManager.ConnectionStrings["NorthWind"].ToString()))
            {
                using(SqlDataAdapter da = new SqlDataAdapter(strsql,conn))
                {
                    da.Fill(ds, startIndex, pageSize, "Customers");
                }
            }
            return ds;
        }

        public int GetCount(string sortE)
        {
            return DataCount;
        }
    }
}
bubuko.com,布布扣

Tip:通过DataAdapter对象的Fill(dataSet,startIndex,pageSize,tableName)方法获取分页数据。

 

 

#排序实现过程细节

1、声明排序字段vsSortColumn及排序方向vsSortDirection两个字段,设置默认值。代码如下:

bubuko.com,布布扣
public string DefaultSortColumn = "CustomerID";
public string DefaultSortDirection = "ASC";
/// <summary>  
///排序字段  
/// </summary>  
public string vsSortColumn
{
  set { this.ViewState["SortColumn"] = value; }
  get { return ((string)(this.ViewState["SortColumn"] ?? DefaultSortColumn)); }
}
/// <summary>  
///排序方向  
/// </summary>  
public string vsSortDirection
{
  set { this.ViewState["SortDirection"] = value; }
  get { return ((string)(this.ViewState["SortDirection"] ?? DefaultSortDirection)); }
}
bubuko.com,布布扣

2、添加ObjectDataSource的OnSelecting事件

bubuko.com,布布扣
//排序,其中strSortExpression传递给sortE
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
  string strSortExpression = "";
  strSortExpression = this.vsSortColumn + " " + this.vsSortDirection;
  e.InputParameters["sortE"] = strSortExpression;
}
bubuko.com,布布扣

e.InputParameters通过索引器["sortE"]访 问<SelectParameter>中的<Parameter Name="sortE" Type="String">,并且SelectMethod与SelectCountMethod中设置调用的函数也必须带有< SelectParameter>中的参数。

3、添加ListView1_Sorting事件

bubuko.com,布布扣
protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
        {
            if (this.vsSortColumn == e.SortExpression)
            {
                if (this.vsSortDirection == "ASC")
                {
                    this.vsSortDirection = "DESC";
                }
                else
                {
                    this.vsSortDirection = "ASC";
                }
            }
            else
            {
                this.vsSortColumn = e.SortExpression;
                this.vsSortDirection = "ASC";
            }
            e.Cancel = true;
            this.ListView1.DataBind();
        }
bubuko.com,布布扣

 

 

HTML代码如下:

bubuko.com,布布扣
<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1" 
            onsorting="ListView1_Sorting" >
    <LayoutTemplate>
    <table>
        <thead>
            <tr>
                <th><asp:LinkButton ID="LinkButton1" CommandName="Sort" CommandArgument="CustomerID" 
runat
="server">CustomerID</asp:LinkButton></th> <th><asp:LinkButton ID="LinkButton2" CommandName="Sort" CommandArgument="CompanyName"
runat
="server">CompanyName</asp:LinkButton></th> <th><asp:LinkButton ID="LinkButton3" CommandName="Sort" CommandArgument="ContactName"
runat
="server">ContactName</asp:LinkButton></th> <th><asp:LinkButton ID="LinkButton4" CommandName="Sort" CommandArgument="ContactTitle"
runat
="server">ContactTitle</asp:LinkButton></th> <th><asp:LinkButton ID="LinkButton5" CommandName="Sort" CommandArgument="Address"
runat
="server">Address</asp:LinkButton></th> <th><asp:LinkButton ID="LinkButton6" CommandName="sort" CommandArgument="City"
runat
="server">City</asp:LinkButton></th> </tr> </thead> <tbody> <asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder> </tbody> </table> </LayoutTemplate> <ItemTemplate> <tr> <td><%# Eval("CustomerID") %></td> <td><%# Eval("CompanyName")%></td> <td><%# Eval("ContactName")%></td> <td><%# Eval("ContactTitle")%></td> <td><%# Eval("Address")%></td> <td><%# Eval("City")%></td> </tr> </ItemTemplate> </asp:ListView> <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1" PageSize="10"> <Fields> <asp:TemplatePagerField> <PagerTemplate><%=DataCount%>条记录, 当前第<asp:Label runat="server" ID="CurrentPageLabel"
          Text
="<%# Container.TotalRowCount>0 ? (Container.StartRowIndex / Container.PageSize) + 1 : 0 %>"
          
/>/<asp:Label runat="server" ID="TotalPagesLabel"
          Text
="<%# Math.Ceiling ((double)Container.TotalRowCount / Container.PageSize) %>" />页, </PagerTemplate> </asp:TemplatePagerField> <asp:NextPreviousPagerField ShowFirstPageButton="True" ShowNextPageButton="False" /> <asp:NumericPagerField /> <asp:NextPreviousPagerField ShowFirstPageButton="False" ShowLastPageButton="True" ShowPreviousPageButton="False" /> </Fields> </asp:DataPager> </div> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="ContractDAL.LAO" SelectMethod="GetData" EnablePaging="True" SelectCountMethod="GetCount" MaximumRowsParameterName="pageSize" StartRowIndexParameterName="startIndex" OnSelecting="ObjectDataSource1_Selecting" > <SelectParameters> <asp:Parameter Name="sortE" Type="String"/> </SelectParameters> </asp:ObjectDataSource>
bubuko.com,布布扣

Tip:ListViewSortEventArgs.SortExpression 属性中存的是<ListView>下的CommandArgument属性,每次点击带有CommandName="Sort"的标 签,CommandName中的值不分大小写,会将CommandArgument的值存在 ListViewSortEventArgs.SortExpression中。

ListView与ObjectDataSource实现CRUD及分页排序细节,布布扣,bubuko.com

ListView与ObjectDataSource实现CRUD及分页排序细节

原文:http://www.cnblogs.com/xuq23/p/3620225.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!