我需要使用asp.net将一个字符串数组存储在我的webform中的HiddenField中.有人可以告诉我如何才能实现呢?谢谢
解决方法
可能有几种方法可行.
1)在JSON中序列化String []
这在使用JavaScriptSerializer类的.NET中相当容易,并避免了分隔符字符的问题.就像是:
String[] myValues = new String[] { "Red","Blue","Green" }; string json = new JavaScriptSerializer().Serialize(myValues);
2)使用不会出现在字符串中的分隔符
使用|||等字符分隔每个字符串这将不会出现在字符串中.您可以使用String.Join()来构建此字符串.就像是:
String[] myValues = new String[] { "Red","Green" }; string str = String.Join("|||",myValues);
然后重建它像:
myValues = str.Split(new string[] { "|||" },StringSplitOptions.RemoveEmptyEntries);
如果您可以信任您的输入,例如一系列预定义的选择,这可能是最佳选择.否则,您可能想要检查输入字符串,以确保它们不包含此分隔符,如果您想要非常安全.您可以使用HttpUtility.HtmlEncode()来首先转义每个字符串.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。