2015-12-29 77 views
0

我有一个DataList来显示吨的项目,所以我使用分页和搜索方法,以便过滤和使它更容易,两者都工作正常。然而,搜索后,当涉及到传呼,数据将回(SELECT *),但没有具体的项目IM搜索DataList分页与搜索结果

是我迄今所做的:

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

搜索和呼叫正在没有彼此罚款。但我希望他们一起工作。感谢

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

的情况下,里克 - 需要更多的信息 the errors occurred

+0

按钮单击后你绑定未过滤的数据'databind()' – Rik

+0

我注意到,但我不知道该如何修复该问题 – TheButterfly

+0

让你的数据绑定应用过滤器(如果有的话) – Rik

回答

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"]

+0

有趣的代码,但它显示我filterSource和filterEvents尚未申报,所以我应该? – TheButterfly

+0

你编辑的按钮上有2个viewstate,我相信按钮中的单词过滤属于数据绑定吧?它需要被声明或应该是filter = this.ViewState [“filter”]; ??? – TheButterfly

+0

对不起,我的时间有点不足。你也应该对viewstate过滤器进行一些检查。 Viewstate是一个数组,所以没关系。将于今天下午更新。希望这会让你走。 – Rik

0

您可以创建一个通用的数据绑定功能,将期望参数搜索也。然后到处使用这个功能。

+0

你的意思是我实际上可以在数据绑定中设置“WHERE item = ???”,所以当我搜索和分页时没有问题?我厌倦了,但不幸的是即时通讯使用e.command(“过滤器”),所以即时通讯不能作为 – TheButterfly