简体   繁体   中英

save dropdown value in viewstate

i create custom page that shows articles,I added dropdown where i can select the sort order,and also I realized custom paging. here is the code

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();
}

I add autoPostBack ='true' to dropdown,and wneh I choose from dropdown , my articles are sorted, but the selected value from dropdown doesn't saves when I go to next page(because i create dropdown every time when page is created) how can I save the dropdown value into viewstate? I also set EnableViewState="True" ViewStateMode="Enabled" on dropdown

ViewState is saved in page body, so when you go to different page new ViewState is created. To save a value between going different pages use Session , Cookies or QueryString .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM