0

我创建了显示文章的自定义页面,我添加了可以选择排序顺序的下拉菜单,还实现了自定义分页。这是代码

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} '>&lt; 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 &gt;</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"了下拉

4

1 回答 1

1

ViewState保存在页面正文中,因此当您转到不同的页面时,ViewState会创建新的页面。要在不同页面之间保存值,请使用Session,CookiesQueryString

于 2013-10-18T09:24:46.380 回答