本帖最后由 chase_wang 于 2013-04-06 20:23:54 编辑

解决方案 »

  1.   

    后台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 System.Data.Sql;
    using System.Data.SqlClient;
    using Eos.Class;public partial class BPage_UserAdmin : System.Web.UI.Page
    {    protected static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态
        public static string x;
        public static string mySql;
        EosClass gl = new EosClass();
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!IsPostBack)
            {
                BindDataList(0);
                gl.CloseSqlConnect();
            }
        }
        //对datelist进行数据绑定
        //-----------------------------------------------------------商品显示-------------------------------------------------------------------
        private void BindDataList(int currentpage)
        {
            pds.AllowPaging = true;//允许分页
            pds.PageSize = 10;//每页显示5条数据
            pds.CurrentPageIndex = currentpage;//当前页为传入的一个int型值        
            //定义查询语句,这里最好将SQL语句在SQL中写好并验证正确确在复制粘贴过来(在对数据查询时最好只查所需的一些不需要的数据就不要取出,这样可以提高运行的效率)
           
            mySql = "SELECT * FROM users order by id asc ";//定义一条SQL语句
                    
            DataSet ds = gl.myDataSet(mySql);
            pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中
            DataList.DataSource = pds;//绑定Datalist
            DataList.DataBind();
            gl.CloseSqlConnect();    }
        protected void DataList_ItemCommand(object source, DataListCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                //以下5个为 捕获用户点击 上一页 下一页等时发生的事件
                case "first"://第一页
                    pds.CurrentPageIndex = 0;
                    BindDataList(pds.CurrentPageIndex);
                    break;
                case "pre"://上一页
                    pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
                    BindDataList(pds.CurrentPageIndex);
                    break;
                case "next"://下一页
                    pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
                    BindDataList(pds.CurrentPageIndex);
                    break;
                case "last"://最后一页
                    pds.CurrentPageIndex = pds.PageCount - 1;
                    BindDataList(pds.CurrentPageIndex);
                    break;
                case "search"://页面跳转页
                    if (e.Item.ItemType == ListItemType.Footer)
                    {
                        int PageCount = int.Parse(pds.PageCount.ToString());
                        TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
                        int MyPageNum = 0;
                        if (!txtPage.Text.Equals(""))
                            MyPageNum = Convert.ToInt32(txtPage.Text.ToString());
                        if (MyPageNum <= 0 || MyPageNum > PageCount)
                        {
                            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请输入正确的页码!');", true);
                            txtPage.Text = "";
                        }
                        else
                            BindDataList(MyPageNum - 1);
                    }
                    break;
           
            }
        }
        protected void DataList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Footer)
            {
                //以下六个为得到脚模板中的控件,并创建变量.
                Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;
                Label PageCount = e.Item.FindControl("labPageCount") as Label;
                LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;
                LinkButton PrePage = e.Item.FindControl("lnkbtnFront") as LinkButton;
                LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;
                LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;
                CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();//绑定显示当前页
                PageCount.Text = pds.PageCount.ToString();//绑定显示总页数
                if (pds.IsFirstPage)//如果是第一页,首页和上一页不能用
                {
                    FirstPage.Enabled = false;
                    PrePage.Enabled = false;
                }
                if (pds.IsLastPage)//如果是最后一页"下一页"和"尾页"按钮不能用
                {
                    NextPage.Enabled = false;
                    LastPage.Enabled = false;
                }
            }
        }
        protected void DataList_DeleteCommand(object source, DataListCommandEventArgs e)
        {
           
            x = ((Label)e.Item.FindControl("LB_id")).Text.Trim();
            mySql = "delete from  users  where id='" + x + "'";        SqlCommand scm = gl.mySqlCommand(mySql);        BindDataList(0);
        }
        protected void DataList_EditCommand(object source, DataListCommandEventArgs e)
        {
            DataList.EditItemIndex = e.Item.ItemIndex;
            BindDataList(0);
        }
     
        protected void DataList_UpdateCommand(object source, DataListCommandEventArgs e)
        {
             string a = ((TextBox)e.Item.FindControl("TB_username")).Text.Trim();
             x=((Label)e.Item.FindControl("LB_id1")).Text.Trim();
             mySql = "update users set username='"+a+"' where id='"+x+"'";        SqlCommand scm = gl.mySqlCommand(mySql);
           
            BindDataList(0);
        }
        protected void DataList_CancelCommand(object source, DataListCommandEventArgs e)
        {
            DataList.EditItemIndex = -1;
            BindDataList(0);
        }
    }
      

  2.   

    Refer:
    http://www.cnblogs.com/insus/articles/1443147.htmlsee also:
    http://www.cnblogs.com/insus/archive/2012/03/08/2385223.html
      

  3.   

    如果你看实际的教程,例如 http://www.cnblogs.com/eddie005/archive/2006/08/18/EventsOfDataWebControl.html
    如果你进行真正的测试你就会发现,人家在开始进入编辑Edit状态时根本不读取数据库。而你的程序呢?竟然要 BindDataList(0);
      

  4.   

    不管你的问题出在哪里,你把握一点:“不要胡乱Bind数据库”这就能迅速发现你所编写asp.net程序的bug。
      

  5.   


    我后来修改好了, 是数据库操作的问题. 修改下好了.  updatee delete事件的写法是对的.很谢谢你.