2013-03-28 30 views
0

在GridView中顯示的數據我已經把我的頁面上的GridView和它連接到LinqDataSourceFemale(數據庫的女表),我有如下一個上單擊事件,搜索頁面

protected void ButtonSearch_Click(object sender, EventArgs e) 
    { 
     using(BerouDataContext Data = new BerouDataContext()) 
     { 
       if (DropDownListGender.SelectedItem.Text == "Male") 
       { 

        int age = Convert.ToInt32(DropDownListAge.Text); 
        string education = DropDownListEducation.Text.ToString(); 
        string maritalstatus = DropDownListMaritalStatus.Text.ToString(); 
        //var religion = DropDownListReligion.Text.ToString(); 
        string caste = DropDownListCaste.Text.ToString(); 
        string city = DropDownListCity.ToString(); 

        var SearchResultBoys = Data.Males.Where(tan => 
         (tan.Age == age) 
         && (tan.Education == education) 
         && (tan.Group == maritalstatus) 
         && (tan.Caste == caste)); 

        GridViewMale.DataSourceID = ""; 
        GridViewMale.DataSource = SearchResultBoys; 
        GridViewMale.DataBind(); 
       } 
       else if (DropDownListGender.SelectedItem.Text == "Female") 
       { 
        int age = Convert.ToInt32(DropDownListAge.Text); 
        string education = DropDownListEducation.Text.ToString(); 
        string maritalstatus = DropDownListMaritalStatus.Text.ToString(); 
        //var religion = DropDownListReligion.Text.ToString(); 
        string caste = DropDownListCaste.Text.ToString(); 
        string city = DropDownListCity.ToString(); 

        var SearchResultGirls = Data.Females.Where(tan => 
         (tan.Age == age) 
         && (tan.Education == education) 
         && (tan.Group == maritalstatus) 
         && (tan.Caste == caste)); 

        GridViewFemale.DataSourceID = ""; 
        GridViewFemale.DataSource = SearchResultGirls; 
        GridViewFemale.DataBind(); 



       } 
     } 
    } 

搜索事件類似代碼網格視圖不會出現在按鈕點擊後,請幫助我。

+0

可能是回傳的事情。你在動態創建網格嗎? –

+0

我已經把網格放在一個頁面上,並將其鏈接到一個數據庫表,並在代碼中,我已經做到了 - > GridViewFemale.DataSource = SearchResultsGirls; ,GridViewFemale.DataBind(); – VINNUSAURUS

+0

LinqDataSourceFemale,GridViewFemale戶外的人。 http://10rem.net/articles/net-naming-conventions-and-programming-standards---best-practices –

回答

1

你必須堅持以某種方式的數據。看起來在按鈕被點擊之後,發生回發並且你失去它。有一件事你可以檢查數據綁定在哪裏發生,確保它在後續回發中被捕獲。

你可以做的就是簡單的數據存儲在一個會話變量的另一件事情,並在回傳重新綁定的數據在GridView。

當數據被第一次檢索,你可以把它分配給會話變量:

Session.Add("searchResultBoys", SearchResultBoys); 
Session.Add("searchResultGirls", SearchResultGirls); 

然後例如在subsuquent pageloads你可以:

GridViewMale.DataSource = (DataTable)Session[searchResultBoys]; //be sure to cast whatever the datasource is, in my example I just used DataTable 
GridViewFemale.DataSource = (DataTable)Session[searchResultGirls]; 

編輯:

所以爲了在會話變量持久化數據,我們對VAR SearchResultBoys和SearchResultGirls保存到一個類型(在這種情況下,數據表)。因爲在會話中保存var會保存查詢表達式而不是結果集。嘗試將您的VAR SearchResultBoys和SearchResultGirls這樣:

IEnumerable<DataRow> SearchResultsBoys = Data.Males.Where(tan => 
      (tan.Age == age) 
      && (tan.Education == education) 
      && (tan.Group == maritalstatus) 
      && (tan.Caste == caste)); 

那麼我們能做的是,結果分配給一個數據表 - 它可以存儲在內存中)和堅持。

DataTable dt = SearchResultsBoys.CopyToDataTable<DataRow>(); 

現在,您可以將數據綁定像以前一樣:

GridViewMale.DataSourceID = ""; 
GridViewMale.DataSource = SearchResultBoys; 
GridViewMale.DataBind(); 

一旦爲你工作的男孩和女孩的數據,那麼我可以告訴你如何使用會話或全局變量堅持。

+0

能否請你在我上面的代碼中添加此代碼告訴我這一點。 – VINNUSAURUS

+0

OOPS對不起,它正在處理我的代碼,我所做的是分配dropdownlist值來搜索和不使用它們。但是,謝謝:) – VINNUSAURUS

0

刪除此GridViewFemale.DataSourceID = "";

而且你在頁面加載綁定gridview!IsPostBack裏面?

如果沒有綁定,請在裏面綁定gridviewIsPostBackpageload事件

編輯

的編碼

protected void Page_Load(object sender, EventArgs e) 
{ 
if(!IsPostBack) 
{ 
// here is Bind gridview code . 
} 
} 
+0

你能告訴我這樣做的代碼。 – VINNUSAURUS

+0

請參閱我編輯的代碼 –