【问题标题】:Displaying Data in GridView on click event, Search Page在点击事件、搜索页面时在 GridView 中显示数据
【发布时间】:2013-03-28 18:43:07
【问题描述】:

我在我的页面上放置了一个 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 并将其链接到一个数据库表,并且在代码中我已经这样做了 --> GridViewFemale.DataSource = SearchResultsGirls; , GridViewFemale.DataBind();
  • LinqDataSourceFemale,GridViewFemale 哦,伙计。 10rem.net/articles/…

标签: c# asp.net linq search gridview


【解决方案1】:

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

您可以做的另一件事是将数据简单地存储在会话变量中,并在回发时重新绑定 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();

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

【讨论】:

  • 你能通过在我上面的代码中添加这个代码来告诉我这个吗?
  • OOPS 很抱歉它在使用我的代码时遇到了麻烦,我所做的是将下拉列表值分配给搜索而不是使用它们。但是谢谢你:)
【解决方案2】:

删除此GridViewFemale.DataSourceID = "";

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

如果没有绑定,请将gridview绑定到pageload事件中的!IsPostBack里面

编辑

编码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    相关资源
    最近更新 更多