我想在我从List创建的网格视图上启用页面索引。
列表如下所示
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后它也无法正常工作,
对可能发生的事情有所了解?有人可以解释我如何将列表结果绑定到网格视图页面索引?
感谢。
答案 0 :(得分: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();
}