在 C# ASP.NET 中维护跨站点状态

发布于 2024-09-09 04:36:07 字数 352 浏览 3 评论 0原文

我有一个网站,它返回 Twitter 的搜索结果,用 C# ASP.NET 编写。搜索效果非常好。

当用户看到结果时,我希望他们也有一个“下一页”类型的超链接,该链接将从上一个结果开始执行搜索(使用 Twitter 的 next_page 数据)。

如何保留属性,以便在单击链接时它将使用不同的参数再次运行搜索以获得下一个结果?我无法使用表单,因为页面上已经有一个表单,而 MS 限制每页一个表单(对于 runat="server")。

请帮忙,这让我发疯。

PS 我想过包含代码,但不确定包含哪一部分,因为它与 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

淡淡の花香 2024-09-16 04:36:07

有一百种不同的方法可以解决这个问题。 ASP.NET 基础结构包含名为 ViewState 的内容,它允许页面及其在页面视图中持久保存任意数据和对象的控件。

有一个

,但您可以拥有无​​限数量的链接和按钮,它们以不同的方式提交表单 - 当页面 回帖

在您的情况下利用此功能的一个简单方法是将页面参数存储在“下一页”链接上。这是一个非常简单的示例,假设您只需要知道页码,但它说明了这一点:

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

在本例中,我们将 LinkBut​​ton 的 CommandArgument 属性设置为我们的页面索引。想。 LinkBut​​ton 触发 NextPage_Click 方法被调用,并且 ASP.NET 的 ViewState 基础结构允许保留 CommandArgument 值。

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.

纸伞微斜 2024-09-16 04:36:07

有两种简单的方法可以做到这一点:

在 url 中包含一个参数,即下一个超链接的 href,该 url 可能如下所示:

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

然后您可以从后面的代码中的查询字符串访问该参数。

您还可以将其保存到会话中:

Session["currentPage"] = 1;

然后在回发时您可以检查它:

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

使用会话它会自动过期,具体取决于您的 IIS 设置,而使用查询字符串选项它不会过期(尽管用户可以弄乱它,因此您可以在使用之前必须对其进行验证)。

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).

っ左 2024-09-16 04:36:07

让您的属性将它们的值保存到视图状态中。

像这样的东西:

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

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;
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文