如何通过查询字符串发送数组转换回数组

编程入门 行业动态 更新时间:2024-10-24 05:20:45
本文介绍了如何通过查询字符串发送数组转换回数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在1.aspx中

In 1.aspx

string[] _array = new string[2]; _array[0]="field 1"; _array[1]="field 2"; Response.Redirect("2.aspx?file=" + _array);

在2.aspx中

In 2.aspx

string[] _array; _array = (string[])Request.QueryString["file"];

错误: 无法将类型字符串"转换为字符串[]"

error: Cannot convert type ''string'' to ''string[]''

推荐答案

我认为您无法直接通过查询字符串传递字符串[]. 而是尝试像这样的会话变量. 在第1页中 I think you cannot pass string[] directly through query string. instead try with session variable like this.. In Page 1 string[] _array = new string[2]; _array[0]="field 1"; _array[1]="field 2"; Session["StrArray"] = _array; Response.Redirect("2.aspx");

在第2页

In Page 2

string[] strArray = (string[])(Session["StrArray"]);

希望对您有所帮助.

Hope this helps..

在2.aspx中: In 2.aspx: string _string; _string = (string)Request.QueryString["file"];

您只是从QueryString而不是数组中提取字符串. 问候, Eduard

You are just pulling a string from the QueryString and not an array. Regards, Eduard

如果必须这样做,则需要将字符串数组转换为单个字符串,并在完成后将其转换回.最明显的方法是使用一个不会出现在字符串中的分隔符(也许是"|"),并将数组元素组合为一个字符串.然后,您可以使用String.Split将字符串稍后转换回数组. If you must do that, then you will need to convert the string array into a single string and convert it back when you are done. The most obvious way is to use a separator character that does not appear in your string ("|" perhaps) and combine the array elements into a single string . You can then use String.Split to convert the string back to an array later.

更多推荐

如何通过查询字符串发送数组转换回数组

本文发布于:2023-10-23 00:08:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519149.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!