繁体   English   中英

单击事件,搜索页上的GridView中显示数据

[英]Displaying Data in GridView on click event, Search Page

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



              }
        }
    }

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

您必须以某种方式保留数据。 似乎在单击按钮后,会发生回发,而您会丢失它。 您可能要检查数据绑定在何处触发的一件事,确保它在后续的回发中被捕获。

您可以做的另一件事就是简单地将数据存储在会话变量中,并在回发后将gridview与数据重新绑定。

首次检索数据时,可以将其分配给会话变量:

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

然后,例如在随后的页面加载中,您可以:

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();

一旦这对您的女孩和男孩数据起作用,那么我可以向您展示如何使用会话或全局变量进行持久化。

删除此GridViewFemale.DataSourceID = ""; 线

并且您是否在!IsPostBack内部的页面加载中绑定了gridview

如果未绑定,请在里面绑定gridview pageload事件中的IsPostBack

编辑

的编码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM