存储字符串数组在HiddenField asp.net [英] storing an array of strings in a HiddenField asp.net

查看:323
本文介绍了存储字符串数组在HiddenField asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要存储字符串在HiddenField数组在我的网页表单与asp.net。有人能告诉我怎样才能做到这一点?谢谢

I need to store an array of string in a HiddenField in my webform with asp.net. Can anybody please tell me how I can achieve that? Thanks

推荐答案

大概的一些方法是可行的。

Probably a few methods would work.

1)序列化的String []在JSON

这将使用的JavaScriptSerializer 类在.NET相当容易,并避免与分隔符的问题。是这样的:

This would be fairly easy in .NET using the JavaScriptSerializer class, and avoid issues with delimiter characters. Something like:

String[] myValues = new String[] { "Red", "Blue", "Green" };
string json = new JavaScriptSerializer().Serialize(myValues);

2)拿出从未出现在字符串分隔符

划定每个字符串的字符,如 ||| ,将永远不会出现的字符串中。您可以使用的string.join()来建立这个字符串。是这样的:

Delimit each string with a character such as ||| that will never appear in the string. You can use String.Join() to build this string. Something like:

String[] myValues = new String[] { "Red", "Blue", "Green" };
string str = String.Join("|||", myValues);

然后重建它像:

myValues = str.Split(new string[] { "|||" }, StringSplitOptions.RemoveEmptyEntries);

这可能是,如果你能信任你的输入,如一系列的pre-定义选择号码的最佳选择。否则,你可能想的检查的你的输入字符串,以确保它们不包含该分隔符,如果你想成为非常安全的。你可能使用 HttpUtility.HtmlEn code()先逃跑每个字符串。

This might be the best option if you can trust your input, such as a series of numbers of pre-defined choices. Otherwise, you'd probably want to check your input strings to make sure they don't contain this delimiter if you wanted to be very safe. You could potentially use HttpUtility.HtmlEncode() to escape each string first.

这篇关于存储字符串数组在HiddenField asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆