在 C# ASP.NET 中维护跨站点状态
我有一个网站,它返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有一百种不同的方法可以解决这个问题。 ASP.NET 基础结构包含名为 ViewState 的内容,它允许页面及其在页面视图中持久保存任意数据和对象的控件。
有一个
在您的情况下利用此功能的一个简单方法是将页面参数存储在“下一页”链接上。这是一个非常简单的示例,假设您只需要知道页码,但它说明了这一点:
...
在本例中,我们将 LinkButton 的 CommandArgument 属性设置为我们的页面索引。想。 LinkButton 触发
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:
...
In this example we set the
CommandArgument
property of the LinkButton to the page index we want. The LinkButton triggers theNextPage_Click
method to be called, and ASP.NET's ViewState infrastructure allows the CommandArgument value is persisted.有两种简单的方法可以做到这一点:
在 url 中包含一个参数,即下一个超链接的 href,该 url 可能如下所示:
然后您可以从后面的代码中的查询字符串访问该参数。
您还可以将其保存到会话中:
然后在回发时您可以检查它:
使用会话它会自动过期,具体取决于您的 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:
you can then access that parameter from the querystring in your code behind.
You can also save it to Session:
then upon postback you can check for it:
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).
让您的属性将它们的值保存到视图状态中。
像这样的东西:
Have your properties save their values to the viewstate.
Something like this: