2

我想在从列表创建的网格视图上启用页面索引。

列表如下所示

public class SearchResults
    {
        public string fname { get; set; }
        public string mname { get; set; }
        public string lname { get; set; }

    }

我通过以下方法将数据添加到此列表和 GridView:

SearchResults result= new SearchResults();// Class Variable      
Void Search()
{

result.fname="John";
result.mmane="Cody";
result.lmane="lname";
SearchResults.Add(result);
Gridview1.DataSource=result;
Gridview1.DataBnd();
}

因为,我想为此页面启用分页索引,所以我为其创建了一个事件,如下所示

protected void On_PageIndexChanging(Object sender, GridViewPageEventArgs e)
    {

        GridView1.PageIndex = e.NewPageIndex;
        Gridview1.DataSource=result;
        Gridview1.DataBnd();


    }

即使在页面索引事件中绑定gridview之后它也不起作用,

知道可能会发生什么吗?有人可以解释一下如何将列表结果绑定到网格视图页面索引吗?

谢谢。

4

1 回答 1

0

您可以使用以下代码:

public class SearchResults
{
   public string fname { get; set; }
   public string mname { get; set; }
   public string lname { get; set; }
}

在 .aspx 页面中像这样设置您的标记:

<asp:GridView runat="server" ID="Gridview1" AllowPaging="True" OnPageIndexChanging="PageIndexChanging" PageSize="4">
    </asp:GridView>

在 (.aspx.cs) 后面的代码中编写以下代码:

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
         // Get list of search and bind data with Grid
         this.Search();
      }
    }

    private void  Search()
    {
        var searchResults = new List<SearchResults>();
        var search = new SearchResults { fname = "John" };
        searchResults.Add(search);
        search = new SearchResults { fname = "John2" };
        searchResults.Add(search);

        search = new SearchResults {fname = "Micheal" };
        searchResults.Add(search);

        search = new SearchResults { fname = "Sara" };
        searchResults.Add(search);

        search = new SearchResults { fname = "Jenne" };
        searchResults.Add(search);

        search = new SearchResults { fname = "Mac" };
        searchResults.Add(search);

        this.Gridview1.DataSource = searchResults;
        this.Gridview1.DataBind();
    }

    protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.Gridview1.PageIndex = e.NewPageIndex;
        this.Search();
    }
于 2013-11-27T05:01:35.850 回答