繁体   English   中英

带有搜索结果的DataList分页

[英]DataList paging with search result

我有一个DataList来显示大量项目,因此我使用分页和搜索方法来进行过滤并使其变得更容易,两者都可以正常工作。 但是,在搜索后进行分页时,数据将返回到(SELECT *),而不是搜索特定项目

我到目前为止所做的:

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

过滤器部分显示如下

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

我使用的按钮:

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

搜索和分页不能彼此正常工作。 但我希望他们一起工作。 谢谢

*******更新*******

万一Rik需要更多信息 发生错误

让您进行数据绑定应用过滤器(如果有)

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

然后过滤命令:

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

和你的按钮

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

确保this.ViewState["filter"] (您可以执行与this.ViewState["vs"]

您可以创建一个通用的databind函数,该函数还将期望使用参数进行搜索。 ..然后到处使用此功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM