简体   繁体   中英

Maintaining state across site in C# ASP.NET

I have a website that returns search results from Twitter, written in C# ASP.NET. The search works very well.

When the user sees the results I want them to also have a 'Next Page' type hyperlink that will perform the search from the last previous result onwards (using Twitter's next_page data).

How can I preserve properties so that when a link is clicked it will run the search again with different parameters for the next results? I cannot use Form as there is one Form on the page already and MS limits to one Form per page (for runat="server" ).

Please help, this is driving me nuts.

PS I thought of including code, but not sure which bit to include, as it has more to do with how ASP.NET works as much as how my own coding is.

There are two easy ways to do it:

Include a parameter in the url that is the href of the Next hyperlink, the url could look something like this:

http://mysite.com/myWebPage.aspx?currentPage=1

you can then access that parameter from the querystring in your code behind.

You can also save it to Session:

Session["currentPage"] = 1;

then upon postback you can check for it:

int currentPage = 0;
if (Session["currentPage"] != null)
    int.TryParse(Session["currentPage"].ToString(), out currentPage);

with the Session it will automatically expire depending on your IIS setup, whereas using the querystring option it doesn't expire (although the user can mess with it so you will have to validate it before using it).

There's a hundred different ways to solve this problem. The ASP.NET infrastructure includes something called ViewState , which allows the page and its controls to persist arbitrary data and objects across page views.

There is a single <form> , but you can have an unlimited number of links and buttons which submit the form in different ways - each one can trigger its own method when the page posts back .

A simple way to leverage this in your case is to store the page parameter on the "next page" link. This is a very simple example that assumes you only need to know the page number, but it gets the point across:

<asp:LinkButton runat="server" ID="next_page" Text="Next Page" OnClick="NextPage_Click" />

...

void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        LoadData(0);
    }
}

void LoadData(int page)
{
    //pull page of data from twitter & show on page
    next_page.CommandArgument = (page+1).ToString();
}

void NextPage_Click(object sender, EventArgs e)
{
    int page = int.Parse(((LinkButton)sender).CommandArgument);
    LoadData(page);
}

In this example we set the CommandArgument property of the LinkButton to the page index we want. The LinkButton triggers the NextPage_Click method to be called, and ASP.NET's ViewState infrastructure allows the CommandArgument value is persisted.

Have your properties save their values to the viewstate.

Something like this:

public int PageNumber
{  
     get
     {         
          object value == this.ViewState["PageNumber"];
          if(value != null)
          { 
               return (int)value;
          }
          else
          {
               return 1;
          }
     }
     set
     { 
          this.ViewState["PageNumber"] = value;
     }
}

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