我创建了显示文章的自定义页面,我添加了可以选择排序顺序的下拉菜单,还实现了自定义分页。这是代码
var sortOrder = new List<string> {"Title", "Date", "Author"};
if (!IsPostBack)
{
sortOrderDropDownList.DataSource = sortOrder;
sortOrderDropDownList.DataBind();
}
articles = articles.OrderBy(a => a.Heading).ToList();
if (IsPostBack)
{
switch (sortOrderDropDownList.SelectedItem.Value)
{
case "Date":
articles = articles.OrderByDescending(a => a.StartDate).ToList();
break;
case "Author":
articles = articles.OrderBy(a => a.AuthorComment).ToList();
break;
default:
articles = articles.OrderBy(a => a.Heading).ToList();
break;
}
}
this.ArticleRepeater.DataSource = articles;
if (articles.Count > 10)
{
int count = articles.Count - (number - 1) * 10 >= 10 ? 10 : articles.Count - (number - 1) * 10 - 1;
this.ArticleRepeater.DataSource = articles.GetRange((number - 1) * 10, count);
StringBuilder stringBuilder = new StringBuilder();
if (number > 1)
{
stringBuilder.AppendFormat("<a href='{0}?page={1} '>< Prev</a> | ", Request.Url.AbsolutePath, (number - 1));
stringBuilder.AppendFormat("<b>Page {0}</b>", number);
if (articles.Count > number * 10)
stringBuilder.AppendFormat(" | <a href='{0}?page={1}'>Next ></a>", Request.Url.AbsolutePath, (number + 1));
this.ArticlePaginator.Text = string.Format("<div class='ArticleListPagination'>{0}</div>", stringBuilder);
}
this.ArticleRepeater.DataBind();
}
我添加autoPostBack ='true'
到下拉列表中,然后我从中选择dropdown
,我的文章已排序,但是dropdown
当我转到下一页时,从中选择的值不会保存(因为我每次创建页面时都会创建下拉列表)如何将dropdown
值保存到视图状态?我也设置EnableViewState="True" ViewStateMode="Enabled"
了下拉