简体   繁体   English

我可以通过querystring传递.net对象吗?

[英]Can I pass a .net Object via querystring?

I stucked at a condition , where i need to share values between the pages. 我陷入了一种状况,即我需要在页面之间共享值。 I want to share value from Codebehind via little or no javascript. 我想通过很少或没有JavaScript从Codebehind分享价值。 I already have a question here on SO , but using JS. 我已经在这里有一个关于SO的问题,但是使用JS。 Still did'nt got any result so another approach i am asking. 仍然没有任何结果,所以我要问另一种方法。

So I want to know can i pass any .net object in query string. 所以我想知道我可以在查询字符串中传递任何.net对象。 SO that i can unbox it on other end conveniently. 这样我就可以方便地在另一端拆箱了。

Update 更新

Or is there any JavaScript approach, by passing it to windows modal dialog. 还是有任何JavaScript方法,可以将其传递给Windows模式对话框。 or something like that. 或类似的东西。

What I am doing 我在做什么

What i was doing is that on my parent page load. 我正在做的是在我的父页面加载中。 I am extracting the properties from my class that has values fetched from db. 我正在从类中提取具有从db获取的值的属性。 and put it in a Session["mySession"] . 并将其放在Session["mySession"] Some thing like this. 像这样的东西。

Session["mySession"] = myClass.myStatus which is List<int>;

Now on one my event that checkbox click event from client side, i am opening a popup. 现在,在我的事件中,即客户端单击复选框事件,我打开了一个弹出窗口。 and on its page load, extracting the list and filling the checkbox list on the child page. 并在其页面加载时提取列表并填充子页面上的复选框列表。

Now from here user can modify its selection and close this page. 现在,用户可以从此处修改其选择并关闭此页面。 Close is done via a button called save , on which i am iterating through the checked items and again sending it in Session["mySession"]. 关闭是通过一个名为save的按钮完成的,在该按钮上,我遍历选中的项目,然后再次在Session [“ mySession”]中发送它。

But the problem is here , when ever i again click on radio button to view the updated values , it displays the previous one. 但是问题出在这里,当我再次单击单选按钮以查看更新的值时,它会显示前一个值。 That is , If my total count of list is 3 from the db, and after modification it is 1. After reopening it still displays 3 instead of 1. 也就是说,如果我的数据库列表总数为3,修改后为1。重新打开后,它仍然显示3而不是1。

Yes, you could but you would have to serialize that value so that it could be encoded as a string. 是的,可以,但是您必须序列化该值,以便可以将其编码为字符串。 I think a much better approach would be to put the object in session rather than on the URL. 我认为更好的方法是将对象放在会话中,而不是放在URL上。

I would so something like this. 我会这样的。

var stringNumbers = intNumbers.Select(i => i.ToString()).ToArray();
var qsValue = string.Join(",", stringNumbers);

Request.Redirect("Page.aspx?numbers=" + sqValue);

Keep in mind that if there are too many numbers the query string is not the best option. 请记住,如果数字太多,则查询字符串不是最佳选择。 Also remember that anyone can see the query string so if this data needs to be secure do not use the query string. 还要记住,任何人都可以看到查询字符串,因此,如果此数据需要安全,请不要使用查询字符串。 Keep in mind the suggestions of other posters. 请记住其他海报的建议。

Note 注意

If you are using .NET 4 you can simplify the above code: 如果使用的是.NET 4,则可以简化上面的代码:

var qsValue = string.Join(",", intNumbers);

Make the object serializable and store it in an out-of-process session. 使对象可序列化并将其存储在进程外会话中。

All pages on your web application will then be able to access the object. 然后,Web应用程序上的所有页面都将能够访问该对象。

我可能会使用Cookie来存储对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM