如何将隐藏字段从一个页面传递到另一页面?

发布于 2024-08-05 21:30:57 字数 146 浏览 2 评论 0原文

我有一个.Net 类库。我想从一次代码隐藏页面传递一个隐藏变量,并在另一个代码隐藏页面中获取它。 请注意,我没有任何可以使用表单标签和 get/post 方法的设计页面(aspx 页面)。 我们怎样才能做到这一点?

注意:我想使用隐藏字段将值从一页传递到另一页。

I have a .Net class library. I want to pass a hidden variable from once code behind page and fetch it in another code behind page.
PLease note I dont have any design page(aspx page) where I can use form tag and get/post method.
How can we do this?

NB: I want to use hidden fields to pass value from 1 page to another.

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

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

发布评论

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

评论(5

眼眸里的那抹悲凉 2024-08-12 21:30:57

您可以将值保存在会话中,如下所示:

Session["YourName"] = yourvalue;

在后面的代码中执行此操作:

Session["MyValue"] = "theValueYouWantToPass";

在其他页面中执行以下操作:

string valueFromAnotherPage = Session["MyValue"].ToString();

you can save the value in the Session as such:

Session["YourName"] = yourvalue;

in code behind you do this:

Session["MyValue"] = "theValueYouWantToPass";

in your other page you do:

string valueFromAnotherPage = Session["MyValue"].ToString();
尸血腥色 2024-08-12 21:30:57

如果您想隐藏变量,那么您可以使用会话来存储对象。

例如,

设置会话值

Session["HiddenValue"] = "something";

获取会话值

string something =  (string)Session["HiddenValue"];

但是请记住,会话仅持续有限的时间(这可以通过 IIS 和您的 Web 配置进行配置) 。

这里是了解会话和会话状态的好资源

If you are wanting to keep the variable hidden then you could use a session to store your object.

E.g.,

Setting the session value

Session["HiddenValue"] = "something";

Getting the session value

string something =  (string)Session["HiddenValue"];

Do keep in mind however, that sessions are only for a limited time (this can be configured thorugh IIS and your web configuration).

Here is a good resource to learn about sessions and session state.

眼藏柔 2024-08-12 21:30:57

您可以使用 Session 或 HttpContext.Current.Items 。如果您的值是临时变量,我建议使用 HttpContext.Current.Item 而不是 session,因为一旦当前 HttpContext 消失,它就会消失,但存储在 Session 中的项目将消失在会话过期之前不会被清除。

Session["var"]=value;

var value=Session["var"];

HttpContext.Current.Items["var"]=value;

var value=HttpContext.Current.Items;

You can use Session or HttpContext.Current.Items .If you value is a temporary variable I suggest using HttpContext.Current.Item instead of session since it will be gone as soon as current HttpContext is gone but items that are stored in Session won't be cleared until session is expired.

Session["var"]=value;

var value=Session["var"];

HttpContext.Current.Items["var"]=value;

var value=HttpContext.Current.Items;
涫野音 2024-08-12 21:30:57

您可以使用会话变量吗?它们不是我的偏好,但它会满足您的需求。

Session["VariableName"] = something;

object somethingOnNextPage = Session["VariableName"];

善良,

You could use a Session variable? They are not my preference but it would satisfy your need.

Session["VariableName"] = something;

object somethingOnNextPage = Session["VariableName"];

Kindness,

Dan

音盲 2024-08-12 21:30:57

跨页回发怎么样(请参阅 ASP.NET 网页中的跨页回发,http://msdn.microsoft.com/en-us/library/ms178139.aspx)?从未真正使用过它,但它可能是一个选择。否则,您可以通过旧式的 Request.Form 访问隐藏的表单元素。另一种选择可能是通过将其放在母版页中来始终在每个页面上保留此隐藏元素。然后,您将其公开为公共财产,并可以根据自己的喜好获取/设置它。

What about cross-page postbacks (see Cross-Page Posting in ASP.NET Web Pages, http://msdn.microsoft.com/en-us/library/ms178139.aspx)? Never really used it, but it could be an option. Otherwise, you could access your hidden form element via the old school Request.Form . Another option could be to always have this hidden element on every page by putting it in the master page. Then you expose it as a public property and can get/set it to your heart's content.

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