页面加载时我用GridView手动绑定全部数据,分页也是代码控制的。
用一个DropDownList 根据选项去搜索数据,然后再绑定在GridView里,由于搜索的数据多,肯定要分页的,然后点击下一页时GridView的数据变成原来的全部数据了。能根据DropDownList里的选项进行搜索后绑定数据,之后分页就不行了,各位大哥帮帮忙
代码如下:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;public partial class News_Manage : System.Web.UI.Page
{
    private PagedDataSource pds;
    protected void Page_Load(object sender, EventArgs e)
    {
        pds = new PagedDataSource();
        pds.AllowPaging = true;
        pds.PageSize = 12;
        if (!IsPostBack)
        {
            if (Session["UserName"] != null)
            {
                bind();
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }
    }
    private void bind()
    {
        string str = @"select * from contents order by id desc";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myhouseConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand(str, con);
        con.Open();
        SqlDataAdapter sda = new SqlDataAdapter();
        DataSet ds = new DataSet();
        sda.SelectCommand = cmd;
        sda.Fill(ds, "d2");        //分页方法
        int page = 1;
        if (Request.QueryString["Page"] != null)
            page = Convert.ToInt32(Request.QueryString["Page"]);
        pds.CurrentPageIndex = page - 1;
        pds.DataSource = ds.Tables["d2"].DefaultView;
        GridView1.DataSource = pds;
        GridView1.DataBind();
        if (!pds.IsFirstPage)
        {
            this.First.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
            this.Prce.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + (page - 1);
        }
        if (!pds.IsLastPage)
        {
            this.Next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + (page + 1);
            this.Last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + pds.PageCount;
        }
        this.lblMsg.Text = page + "/" + pds.PageCount;
        con.Close();
        cmd.Dispose();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        int category = Convert.ToInt32(ddlclass.SelectedValue.ToString());
        string str = @"select * from contents where category like '" + Convert.ToString(category) + "0%'";        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myhouseConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand(str, con);
        SqlDataAdapter sda = new SqlDataAdapter();
        DataSet ds = new DataSet();
        con.Open();
        sda.SelectCommand = cmd;
        sda.Fill(ds, "fill");        //分页方法
        int page = 1;
        if (Request.QueryString["Page1"] != null)
            page = Convert.ToInt32(Request.QueryString["Page1"]);
        pds.CurrentPageIndex = page - 1;
        pds.DataSource = ds.Tables["fill"].DefaultView;
        GridView1.EnableSortingAndPagingCallbacks = true;
        GridView1.DataSource = pds;
        GridView1.DataBind();        if (!pds.IsFirstPage)
        {
            this.First.NavigateUrl = Request.CurrentExecutionFilePath + "?Page1=1";
            this.Prce.NavigateUrl = Request.CurrentExecutionFilePath + "?Page1=" + (page - 1);
        }
        if (!pds.IsLastPage)
        {
            this.Next.NavigateUrl = Request.CurrentExecutionFilePath + "?Page1=" + (page + 1);
            this.Last.NavigateUrl = Request.CurrentExecutionFilePath + "?Page1=" + pds.PageCount;
        }        this.lblMsg.Text = page + "/" + pds.PageCount;
        con.Close();
        con.Dispose();
        cmd.Dispose();
    }
}

解决方案 »

  1.   

    private void bind()
        {
            string str = "select * from contents";
    if(Request["key"]!=null) str += " where category like '"+Request["k"].Replace("'","''")+"'";
    str += " order by id desc";
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myhouseConnectionString"].ConnectionString);
            SqlCommand cmd = new SqlCommand(str, con);
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter();
            DataSet ds = new DataSet();
            sda.SelectCommand = cmd;
            sda.Fill(ds, "d2");        //分页方法
            int page = 1;
            if (Request.QueryString["Page"] != null)
                page = Convert.ToInt32(Request.QueryString["Page"]);
            pds.CurrentPageIndex = page - 1;
            pds.DataSource = ds.Tables["d2"].DefaultView;
            GridView1.DataSource = pds;
            GridView1.DataBind();
    string url = Request.CurrentExecutionFilePath;
    if(Request["key"]!=null) url += "?key="+Request["key"]+"&";
    else url += "?";
            if (!pds.IsFirstPage)
            {
                this.First.NavigateUrl = url + "Page=1";
                this.Prce.NavigateUrl = url + "Page=" + (page - 1);
            }
            if (!pds.IsLastPage)
            {
                this.Next.NavigateUrl = url + "Page=" + (page + 1);
                this.Last.NavigateUrl = url + "Page=" + pds.PageCount;
            }
            this.lblMsg.Text = page + "/" + pds.PageCount;
            con.Close();
            cmd.Dispose();
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect("a.aspx?key="+ddlclass.SelectedValue);    }
      

  2.   


    天涯??这里是CSDN
    不过人气确实少了
      

  3.   

    谢谢sq_zhuyi  高手的思路就是不一般啊
      

  4.   

    结贴去了,分数虽然少了点,希望sq_zhuyi别介意