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

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

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

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

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

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;
        }
    }
}

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

 

 

#排序实现过程细节

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

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)); }
}

2、添加ObjectDataSource的OnSelecting事件

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

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

3、添加ListView1_Sorting事件

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();
        }

 

 

HTML代码如下:

<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>

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

 

转载于:https://www.cnblogs.com/xuq23/p/3620225.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值