在这种常见场景中,使用什么在页面之间传递数据
我的场景是用户选择多个选项(用于过滤记录),然后 他点击了一个按钮。结果(从数据库检索的记录)将显示在 新页面。
您能告诉我具体的步骤吗?以及将使用哪种方法来存储数据?和 我将存储哪些数据?
多谢 。
My scenario is that the user selects a number of options (for filtering records) and then
he clicks a button . The result (retrieved records from DB) will be displayed in a
new page .
Would you please show me the steps for that ? and which method will be used to store data ? and
what data I will be storing ?
Thanks a lot .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
理想情况下(并且 REST 方式),该场景可能效果最好,如下所示:
为了增加灵活性,Page2 应检查表单元素和查询字符串元素。 (如果两者都被发送,您需要决定哪个覆盖另一个。)这样,过滤器甚至可以被添加书签或通过电子邮件发送给同事。
没有必要涉及会话、cookie 等内容。这不必要地使问题复杂化,并使场景不太可移植且不太 RESTful。
在这种情况下需要注意的一件事是“页面”一词的使用。虽然这在 WebForms(我假设您正在使用)的思维方式中可能很常见,但您应该明白“网络”实际上没有“页面”的概念。这是一个“资源”的问题。
在本例中,Resource1 的响应(这是对 Page1.aspx 的 GET 请求)是一个对 Resource2(这是对 Page2.aspx 的 POST 请求)执行 POST 操作的表单,该操作会响应一些数据。为了获得最佳设计结果,您应该在心里将“页面”和“资源和请求”的概念分开。
无论如何,回到例子。 Page1 会有类似这样的内容:
您会注意到这是高度简化的。您可以使用 ASP.NET 服务器控件来构建它,也可以在 HTML 中手动构建它,等等。这完全取决于您。就我个人而言,我喜欢使输出尽可能精简,但是您想使用框架工具的程度由您决定。我认为,对于工具支持,您需要研究 ASP.NET 中的“跨页发布”,因为 WebForms 通常假设一切都是回发。我认为在这种情况下,只需在
asp:button
上设置PostBackUrl
即可。在 Page2 的代码隐藏中,您需要查看
Request
对象上的Form
属性。 (更多信息此处。) 与往常一样,如果您使用直接 SQL 查询、错误处理等,您将需要进行输入检查、防止任何 SQL 注入。但基本概念可以完成工作。
Ideally (and RESTfully) the scenario would probably work best as thus:
For added flexibility, Page2 should check both for form elements as well as query string elements. (You'd want to decide which overrides the other, in the event that both are sent.) That way a filter could even be bookmarked or emailed to a colleague.
There's no need to involve things like session, cookies, etc. That needlessly complicates the matter and makes the scenario less portable and less RESTful.
One thing to note in this scenario is the use of the word "page." While this may be commonplace in the mindset of WebForms (which I assume you're using), you should understand that "the web" really doesn't have a notion of "pages." It's a matter of "resources."
In this case, the response for Resource1 (which is a GET request to Page1.aspx) is a form which has a POST action to Resource2 (which is a POST request to Page2.aspx), which responds with some data. For best design results, you should mentally keep the concept of "pages" and "resources and requests" separate.
Anyway, back to the examples. Page1 would have something like:
You'll note that this is highly simplified. You can use the ASP.NET server controls to build this, you can build it manually in the HTML, etc. That's really up to you. Personally I like to keep the output as lean as possible, but how much you want to use the tooling of the framework is your call. I think, for the tooling support, you'd want to look into "Cross-Page Posting" in ASP.NET, since WebForms usually assumes everything is a post-back. I think it's just a matter of setting the
PostBackUrl
on theasp:button
in that case.In the code-behind for Page2, you'd want to look at the
Form
property on theRequest
object. (More information here.) You'd have something like:As always, you'll want to do input checking, prevent any SQL injection if you're using straight SQL queries, error handling, etc. But the basic concept gets the job done.
您可以通过会话传递它,也可以通过 GET 传递。
session:
GET:
注意:
You can pass it with a session or pass with GET.
session:
GET:
note: