在视图状态中保存下拉值

本文关键字:保存 视图状态 | 更新日期: 2023-09-27 18:35:59

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

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"

在视图状态中保存下拉值

ViewState保存在

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