2014-04-14 78 views
0

我是新來的asp.net,需要一些幫助。我有一個GridView,每頁有20條記錄,我在GridView外面有一個搜索按鈕。我需要做的是,當我單擊搜索按鈕時,結果必須綁定到gridview(現在正在發生),但是當記錄超過pagesize並且我需要轉到網格的下一頁時,綁定會丟失,並且綁定的記錄是加載事件中形成頁面的記錄。下面是我的代碼示例。在按鈕上綁定GridView單擊分頁事件

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     BindData(); 
    } 
} 

public void BindData() 
{ 

    { 
     List<EventFile> eventFile = new List<EventFile>(); 
     eventFile = CoMailAssociationDAL.GetUploadFileUnAssigned(0, "", "", "U"); 
     if (gvwAssociation.DataSource == null) 
     { 
      gvwAssociation.DataSource = eventFile; 
      gvwAssociation.DataBind(); 
     } 
    } 
} 
protected void btnSearch_Click(object sender, EventArgs e) 
{ 
    int uFlag = 0; 
    string uploadFlag = this.ddlUploadDate.SelectedValue; 
    string fileName = this.txtSearchText.Text; 
    string uploadDt = this.txtDate.Text; 
    string status = this.ddlStatus.SelectedValue.ToString(); 
    bt = true; 


    if (status == "Un-Assigned") 
    { 
     status = "U"; 
    } 
    else if (status == "Assigned") 
    { 
     status = "A"; 
    } 
    else 
    { 
     status = "B"; 
    } 


    if ((uploadFlag == "On") && (uploadDt == "")) 
    { 
     uFlag = 0; 
    } 
    else if (uploadFlag == "On") 
    { 
     uFlag = 1; 
    } 
    else if (uploadFlag == "OnorBefore") 
    { 
     uFlag = 2; 
    } 
    else 
    { 
     uFlag = 3; 
    } 


    fileSearch = CoMailAssociationDAL.SearchFile(uFlag, fileName, uploadDt, status); 

    gvwAssociation.DataSource = fileSearch; 
    gvwAssociation.DataBind(); 
} 

protected void gvwAssociation_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    //SaveSelectedValues(); 
    gvwAssociation.PageIndex = e.NewPageIndex; 
    //BindData(); 
    //PopulateSelectedValues(); 
} 

回答

2

首先,你應該有尋呼下面的事件處理

protected void gvwAssociation_PageIndexChanging(object sender, GridViewPageEventArgs e) 
{ 
    gvwAssociation.PageIndex = e.NewPageIndex; 
    bindGridWithFilter(); 
} 

然後,搜索按鈕內移動搜索/過濾器邏輯的私有方法(說「bindGridWithFilter」)

提示:嘗試都BindData和bindGridWithFilter結合起來,所以當沒有過濾器,你顯示所有記錄


UPDATE

這裏有一些重構的代碼讓你知道我的意思。

protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!IsPostBack) 
      { 
       bindGridWithFilter(); 
      } 
     } 

     protected void gvwAssociation_PageIndexChanging(object sender, GridViewPageEventArgs e) 
     { 
      gvwAssociation.PageIndex = e.NewPageIndex; 
      bindGridWithFilter(); 
     } 

     protected void btnSearch_Click(object sender, EventArgs e) 
     { 
      bindGridWithFilter(); 

     } 

     private void bindGridWithFilter() 
     { 
      List<EventFile> eventFile = new List<EventFile>(); 
      eventFile = CoMailAssociationDAL.GetUploadFileUnAssigned(0, "", "", "U"); 
      if (gvwAssociation.DataSource == null) 
      { 
       // If you don't have a filter you show all records 
       gvwAssociation.DataSource = eventFile; 
       gvwAssociation.DataBind(); 
      } 
      else 
      { 
       // This is same as the logic in your search button 
       // display only the filtered records 
       int uFlag = 0; 
       string uploadFlag = this.ddlUploadDate.SelectedValue; 
       string fileName = this.txtSearchText.Text; 
       string uploadDt = this.txtDate.Text; 
       string status = this.ddlStatus.SelectedValue.ToString(); 
       bt = true; 


       if (status == "Un-Assigned") 
       { 
        status = "U"; 
       } 
       else if (status == "Assigned") 
       { 
        status = "A"; 
       } 
       else 
       { 
        status = "B"; 
       } 


       if ((uploadFlag == "On") && (uploadDt == "")) 
       { 
        uFlag = 0; 
       } 
       else if (uploadFlag == "On") 
       { 
        uFlag = 1; 
       } 
       else if (uploadFlag == "OnorBefore") 
       { 
        uFlag = 2; 
       } 
       else 
       { 
        uFlag = 3; 
       } 


       List<EventFile> fileSearch = CoMailAssociationDAL.SearchFile(uFlag, fileName, uploadDt, status); 

       gvwAssociation.DataSource = fileSearch; 
       gvwAssociation.DataBind(); 
      } 
     } 

這應該起作用。

+0

感謝您的回答,我已經有了pageindex更改事件的代碼。你能向我展示一些你上面提到的東西的例子嗎?道歉,我真的是asp.net的新手。非常感謝幫助 – Moccassin

+0

我剛更新了我的答案。看一看 – Sam

+0

嗨,山姆,再次感謝您耐心回答我的問題。代碼有點不像預期的那樣工作。顯示所有記錄的條件始終滿足,並且不會進入else語句(用於篩選記錄) – Moccassin