Search code examples
c#asp.netpaginationwebformsdatalist

DataList paging with search result


i have a DataList to display tons of items, so i use paging and search method in order to filter and makes it easier, both are working fine. however, when it comes to paging after search, the data will back to (SELECT *) but not the specific item im searching for

what i done so far:

    SqlDataAdapter adap;
    DataSet ds;
    PagedDataSource adsource;
    int pos;

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.ViewState["vs"] = 0;
            pos = (int)this.ViewState["vs"];

            databind();
            databind2();
        }
     }

public void databind()
    {
        adap = new SqlDataAdapter("select p.item_code as pitem, i.LongDesc as longname, p.SellPrice_1 as normalprice, p.SellPrice_2 as memberprice from plu p inner join item i on p.item_code = i.item_code WHERE p.publish =1 order by i.LongDesc", constr);
        ds = new DataSet();
        adsource = new PagedDataSource();
        adap.Fill(ds);
        adsource.DataSource = ds.Tables[0].DefaultView;
        adsource.PageSize = 16;
        adsource.AllowPaging = true;
        adsource.CurrentPageIndex = pos;
        CategoryList.DataSource = adsource;
        CategoryList.DataBind();
    }

the filter part shows as below

public void Filter_Command(Object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("Filter"))
        {
            adap = new SqlDataAdapter("select p.item_code as pitem, i.LongDesc as longname, p.SellPrice_1 as normalprice, p.SellPrice_2 as memberprice, d.department_code as dcode, d.category_code as dcatecode, c.category_code as ccode from plu p inner join item i on p.item_code = i.item_code inner join EPO_PDU_department d on d.department_code = i.department_code inner join EPO_PDU_Category c on c.category_code = d.category_code WHERE p.publish =1 AND c.category_code = '" + e.CommandArgument.ToString() + "'order by i.LongDesc  ", constr);
            ds = new DataSet();
            adsource = new PagedDataSource();
            adap.Fill(ds);
            adsource.DataSource = ds.Tables[0].DefaultView;
            adsource.PageSize = 16;
            adsource.AllowPaging = true;
            adsource.CurrentPageIndex = pos;
            btnprevious.Enabled = !adsource.IsFirstPage;
            btnnext.Enabled = !adsource.IsLastPage;
            CategoryList.DataSource = adsource;
            CategoryList.DataBind();
        }
    }

buttons that i used:

protected void btnprevious_Click(object sender, EventArgs e)
    {
        pos = (int)this.ViewState["vs"];
        pos -= 1;
        this.ViewState["vs"] = pos;
        databind();
    }

    protected void btnnext_Click(object sender, EventArgs e)
    {
        pos = (int)this.ViewState["vs"];
        pos += 1;
        this.ViewState["vs"] = pos;
        databind();
    }

searching and paging are working fine without each other. but i would like them to work together. thanks

******* UPDATE *******

in case Rik needs more information the errors occurred


Solution

  • Make you databind apply filter, if any

    public void databind(string filter = null)
    {
        var filterQuery = "";
        if(!string.IsNullOrEmpty(filter)){
            filterQuery = " AND c.category_code = '" + filter + "'";
            this.ViewState.ContainsKey("filter") 
                ? this.ViewState["filter"] = filter
                : this.ViewState.Add("filter", filter);
        }
    
        var query = "select p.item_code as pitem, i.LongDesc as longname, p.SellPrice_1 as normalprice, p.SellPrice_2 as memberprice from plu p inner join item i on p.item_code = i.item_code WHERE p.publish =1";
        query += filterQuery;
        query += " order by i.LongDesc";
    
        adap = new SqlDataAdapter(query, constr);
        ds = new DataSet();
        adsource = new PagedDataSource();
        adap.Fill(ds);
        adsource.DataSource = ds.Tables[0].DefaultView;
        adsource.PageSize = 16;
        adsource.AllowPaging = true;
        adsource.CurrentPageIndex = pos;
        CategoryList.DataSource = adsource;
        CategoryList.DataBind();
    }
    

    and then filter command:

    public void Filter_Command(Object source, DataListCommandEventArgs e)
    {
         string filter = e.CommandName.Equals("Filter") ? e.CommandArgument.ToString() : null;
         databind(filter);
    }
    

    and your buttons

    protected void btnprevious_Click(object sender, EventArgs e)
    {
        pos = (int)this.ViewState["vs"];
        pos -= 1;
        this.ViewState["vs"] = pos;
        databind(this.ViewState["filter"]);
    }
    
    protected void btnnext_Click(object sender, EventArgs e)
    {
        pos = (int)this.ViewState["vs"];
        pos += 1;
        this.ViewState["vs"] = pos;
        databind(this.ViewState["filter"]);
    }
    

    Make sure this.ViewState["filter"] is defined (You can do the same as for your this.ViewState["vs"]