DataList分页等源码

cs文件:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MyPersonal.DBUtility;
using System.Data.SqlClient;
public partial class DataListTest : System.Web.UI.Page
{
    string sqlString = "select * from Employees";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["pageIndex"] = 0;
            this.PageFunc();
          
        }
        this.DataList1.SelectedIndexChanged += new EventHandler(DataList1_SelectedIndexChanged);

        this.DataList1.ItemCommand += new DataListCommandEventHandler(DataList1_ItemCommand);

        this.DataList1.EditCommand += new DataListCommandEventHandler(DataList1_EditCommand);

        this.DataList1.CancelCommand += new DataListCommandEventHandler(DataList1_CancelCommand);

        this.DataList1.UpdateCommand += new DataListCommandEventHandler(DataList1_UpdateCommand);

        //上下页激活函数
        this.LinkButton1.Command += new CommandEventHandler(Page_Click);
        this.LinkButton2.Command += new CommandEventHandler(Page_Click);


    }

 

 

    void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        int id = Convert.ToInt32(this.DataList1.DataKeys[e.Item.ItemIndex]);
        string firstName = ((TextBox)e.Item.FindControl("txtFirstName")).Text;
        string updateString = "update Employees set FirstName='" + firstName + "' where EmployeeID='" + id + "'";
        SqlHelper.ExecuteSql(updateString);
        this.DataList1.EditItemIndex = -1;
        this.BindData();
       
      
    }

    void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = -1;
        this.BindData();
     
    }

    void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        this.DataList1.EditItemIndex = e.Item.ItemIndex;
        this.BindData();
    }

    void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName=="select")
        {
            //e.Item.ItemIndex是获取索引,下面的语句是获取DataKeyField是获取相应主键
            Response.Write(this.DataList1.DataKeys[e.Item.ItemIndex].ToString());
           
        }
    }

    void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    
    }

    private void BindData()
    {
        DataTable myTable =SqlHelper.Query1(sqlString);
        this.DataList1.DataSource = myTable;
        this.DataList1.DataBind();
      

    }

    /// <summary>
    /// 分页函数
    /// </summary>
    private void PageFunc()
    {
        PagedDataSource myDataSource = new PagedDataSource();
        myDataSource.DataSource=(SqlHelper.Query1(sqlString)).DefaultView;
        myDataSource.AllowPaging = true;
        myDataSource.PageSize = 3;
        //获取当前页数
        myDataSource.CurrentPageIndex = Convert.ToInt32(ViewState["pageIndex"]);
        if (myDataSource.IsFirstPage)
        {

            this.LinkButton2.Enabled = false;
        }
        else
        {
            this.LinkButton2.Enabled =true;
        }
        if (myDataSource.IsLastPage)
        {

            this.LinkButton1.Enabled= false;
        }
        else
        {
            this.LinkButton1.Enabled = true;
        }
        this.DataList1.DataSource = myDataSource;
        this.DataList1.DataBind();
    }

    void Page_Click(object sender, CommandEventArgs e)
    {
        int pageIndex = int.Parse(ViewState["pageIndex"].ToString());
        if ("next" == e.CommandName)
        {
            pageIndex = pageIndex + 1;

        }
        if ("pre" == e.CommandName)
        {
            pageIndex = pageIndex - 1;

        }
        ViewState["pageIndex"] = pageIndex;
        this.PageFunc();

    }
}

aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataListTest.aspx.cs" Inherits="DataListTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server" GridLines="Both"
            RepeatColumns="3" RepeatDirection="Horizontal" RepeatLayout="Table"
            Height="27px" Width="400px" DataKeyField="EmployeeID">
       
        <ItemTemplate>
      
        <asp:Label ID="lblEmployeeID" runat="server" Text='<%#Eval("EmployeeID") %>'></asp:Label>
       
           <asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
            <asp:Button ID="btnSelect" runat="server" Text="选择" CommandName="select" CommandArgument='<%#Eval("EmployeeID") %>' />
            <asp:Button ID="Button1" runat="server" Text="编辑" CommandName="edit" CommandArgument='<%#Eval("EmployeeID") %>' />
        </ItemTemplate>
        <AlternatingItemStyle  BackColor="Aquamarine"/>
      <AlternatingItemTemplate>
    
        <asp:Label ID="lblEmployeeID" runat="server" Text='<%#Eval("EmployeeID") %>'></asp:Label>
       
           <asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
           <asp:Button ID="btnSelect" runat="server" Text="选择" CommandName="select"  CommandArgument='<%#Eval("EmployeeID") %>'/>
           <asp:Button ID="Button1" runat="server" Text="编辑" CommandName="edit" CommandArgument='<%#Eval("EmployeeID") %>' />
      </AlternatingItemTemplate>
     
      <SelectedItemStyle  BackColor="Red"/>
      <SelectedItemTemplate>
     
        <asp:Label ID="lblEmployeeID" runat="server" Text='<%#Eval("EmployeeID") %>'></asp:Label>
       
           <asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
      </SelectedItemTemplate>
        
        <EditItemTemplate>
       
        <asp:Label ID="lblEmployeeID" runat="server" Text='<%#Eval("EmployeeID") %>'></asp:Label>
        <asp:TextBox ID="txtFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:TextBox>
       <asp:Button ID="btnUpdate" runat="server" Text="更新" CommandName="update" CommandArgument='<%#Eval("EmployeeID") %>' />
        <asp:Button ID="btnCancel" runat="server" Text="取消" CommandName="cancel" CommandArgument='<%#Eval("EmployeeID") %>' />
        </EditItemTemplate>
       
        </asp:DataList>
        <asp:LinkButton ID="LinkButton1" runat="server"   CommandName="next">下一页></asp:LinkButton>
         <asp:LinkButton ID="LinkButton2" runat="server" CommandName="pre">上一页</asp:LinkButton>
       
    </div>
    </form>
</body>
</html>

posted @ 2010-07-18 21:20  雁北飞  阅读(178)  评论(0编辑  收藏  举报