我创建了显示文章的自定义页面,我添加了下拉列表,我可以选择排序顺序,还实现了自定义分页。 这是代码
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
值保存到viewstate中?我还在下拉列表中设置EnableViewState="True" ViewStateMode="Enabled"
答案 0 :(得分:1)
ViewState
保存在页面正文中,因此当您转到其他页面时,会创建新的ViewState
。要在不同页面之间保存值,请使用Session
,Cookies
或QueryString
。