2013-03-28 34 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

请参阅我编辑的代码 –