在这种常见场景中,使用什么在页面之间传递数据

发布于 2024-11-02 21:06:13 字数 123 浏览 0 评论 0原文

我的场景是用户选择多个选项(用于过滤记录),然后 他点击了一个按钮。结果(从数据库检索的记录)将显示在 新页面。

您能告诉我具体的步骤吗?以及将使用哪种方法来存储数据?和 我将存储哪些数据?

多谢 。

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 技术交流群。

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

发布评论

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

评论(2

夏九 2024-11-09 21:06:13

理想情况下(并且 REST 方式),该场景可能效果最好,如下所示:

  1. Page1 有一个表单,其中包含构建过滤器所需的各种选项。
  2. Page1 的表单 POST 到 Page2(也可能是 Page1,但听起来您希望它成为一个单独的页面)。
  3. Page2读取POST数据,执行过滤,显示结果。

为了增加灵活性,Page2 应检查表单元素和查询字符串元素。 (如果两者都被发送,您需要决定哪个覆盖另一个。)这样,过滤器甚至可以被添加书签或通过电子邮件发送给同事。

没有必要涉及会话、cookie 等内容。这不必要地使问题复杂化,并使场景不太可移植且不太 RESTful。

在这种情况下需要注意的一件事是“页面”一词的使用。虽然这在 WebForms(我假设您正在使用)的思维方式中可能很常见,但您应该明白“网络”实际上没有“页面”的概念。这是一个“资源”的问题。

在本例中,Resource1 的响应(这是对 Page1.aspx 的 GET 请求)是一个对 Resource2(这是对 Page2.aspx 的 POST 请求)执行 POST 操作的表单,该操作会响应一些数据。为了获得最佳设计结果,您应该在心里将“页面”和“资源和请求”的概念分开。

无论如何,回到例子。 Page1 会有类似这样的内容:

<form method="POST" action="Page2.aspx">
    <input type="text" name="filterText" />
    <button type="submit" name="filter" value="Filter" />
</form>

您会注意到这是高度简化的。您可以使用 ASP.NET 服务器控件来构建它,也可以在 HTML 中手动构建它,等等。这完全取决于您。就我个人而言,我喜欢使输出尽可能精简,但是您想使用框架工具的程度由您决定。我认为,对于工具支持,您需要研究 ASP.NET 中的“跨页发布”,因为 WebForms 通常假设一切都是回发。我认为在这种情况下,只需在 asp:button 上设置 PostBackUrl 即可。

在 Page2 的代码隐藏中,您需要查看 Request 对象上的 Form 属性。 (更多信息此处。) 与往常一样

if (!string.IsNullOrEmpty(Request.Form.AllKeys["filterText"]))
{
    // Apply your filter when retrieving from the database.  Simple if your database data comes back as a delayed-execution IEnumerable.
}

,如果您使用直接 SQL 查询、错误处理等,您将需要进行输入检查、防止任何 SQL 注入。但基本概念可以完成工作。

Ideally (and RESTfully) the scenario would probably work best as thus:

  1. Page1 has a form with the various options needed to build the filter.
  2. Page1's form POSTs to Page2 (which could also be Page1, but it sounds like you want it to be a separate page).
  3. Page2 reads POST data, performs the filter, displays the results.

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:

<form method="POST" action="Page2.aspx">
    <input type="text" name="filterText" />
    <button type="submit" name="filter" value="Filter" />
</form>

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 the asp:button in that case.

In the code-behind for Page2, you'd want to look at the Form property on the Request object. (More information here.) You'd have something like:

if (!string.IsNullOrEmpty(Request.Form.AllKeys["filterText"]))
{
    // Apply your filter when retrieving from the database.  Simple if your database data comes back as a delayed-execution IEnumerable.
}

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.

海的爱人是光 2024-11-09 21:06:13

您可以通过会话传递它,也可以通过 GET 传递。
session:

Session["Some_Name"] = parameter;
----------------------------------
retrive on the new page
parameter = (casting)Session["Some_Name"];

GET:

Response.Redirect(NewPage.aspx?parm1=p1?parm2=p2); // parm1,parm2 just a name. p1,p2 are paramters
----------------------------------
retrive on the new page
p1 = Request.QueryString["parm1"];
p2 = Request.QueryString["parm2"];

注意:

  • 当您在会话中保存时,参数的类型仍然存在(您可以移动对象)。
  • 当您使用 GET 重定向时,参数将作为字符串移动(您无法移动对象)。
  • 当您使用 GET 重定向时,该参数对位于该地址的用户可见。

You can pass it with a session or pass with GET.
session:

Session["Some_Name"] = parameter;
----------------------------------
retrive on the new page
parameter = (casting)Session["Some_Name"];

GET:

Response.Redirect(NewPage.aspx?parm1=p1?parm2=p2); // parm1,parm2 just a name. p1,p2 are paramters
----------------------------------
retrive on the new page
p1 = Request.QueryString["parm1"];
p2 = Request.QueryString["parm2"];

note:

  • When you save at session the type of the parameter Remains (you can move object).
  • When you redirect with GET the paramter move as string (you can't move object).
  • When you redirect with GET the paramter is visible to the user at the address.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文