0

我在我的页面上放置了一个 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();



              }
        }
    }

单击按钮后不出现网格视图,请帮助我。

4

2 回答 2

1

You have to persist the data in some fashion. It appears that after the button is clicked, a postback occurs and you lose it. One thing you might check on where the databinding fires, ensure that it is caught on subsequent postbacks.

Another thing you could do is simply store the data in a session variable, and upon postback re-bind the gridview with the data.

When the data is first retrieved, you could assign it to a session variable:

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

Then for example on subsuquent pageloads you could:

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

EDIT:

So in order to persist the data in a session variable, we have to save the var SearchResultBoys and SearchResultGirls into a type (in this case a datatable). Becuase saving the var in a session will just save the query expression and not the result set. Try converting your var SearchResultBoys and SearchResultGirls to this:

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

Then what we can do is assign that result to a datatable - where it can be stored in memory) and persisted.

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

Now you can bind the data as before:

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

Once that is working for you on the girls and boys data, then I can show you how to persist using a session or global variable.

于 2013-03-28T19:40:32.577 回答
0

删除此GridViewFemale.DataSourceID = "";

你是绑定gridview在页面加载里面的 !IsPostBack吗?

如果没有绑定,请 gridview 在里面绑定!IsPostBackpageload事件中

编辑

编码为

protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// here is Bind gridview code .
}
}
于 2013-03-28T19:46:57.997 回答