【问题标题】:DataList paging with search result带有搜索结果的 DataList 分页
【发布时间】:2015-12-29 09:08:05
【问题描述】:

我有一个 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();
    }

搜索和分页在没有彼此的情况下工作正常。但我希望他们一起工作。谢谢

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

如果瑞克需要更多信息

【问题讨论】:

  • 点击按钮后,您将绑定未过滤的数据'databind()'
  • 我确实注意到了,但我不确定我应该如何解决这个问题
  • 让您的数据绑定应用过滤器(如果有)
  • @Rik mmmmm,怎么样?你能告诉我你的想法是什么吗?谢谢

标签: c# asp.net webforms paging datalist


【解决方案1】:

让你数据绑定应用过滤器,如果有的话

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"] 执行相同的操作

【讨论】:

  • 有趣的代码,但它告诉我 filterSource 和 filterEvents 尚未声明,我应该这样吗?
  • 您编辑的按钮有 2 个视图状态,我相信按钮中的单词过滤器属于数据绑定,对吗?它需要声明或者应该是 filter = this.ViewState["filter"]; ???
  • 对不起,我的时间有点短。您还应该对视图状态过滤器进行一些检查。 Viewstate 是一个数组,所以没关系。今天下午更新。希望这会让你继续前进。
【解决方案2】:

您可以创建一个通用的数据绑定函数,该函数也需要搜索参数。 ..然后到处使用这个函数。

【讨论】:

  • 你的意思是我实际上可以在数据绑定中设置“WHERE item = ???”,所以当我搜索和分页时会没有问题?我厌倦了,但不幸的是我使用 e.command(“过滤器”)所以我无法做到这一点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 1970-01-01
  • 2014-03-26
  • 2017-02-24
相关资源
最近更新 更多