2015-12-29 105 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