I Have situation need to store an array of string in a HiddenField in my web form with
asp.net.
But It was Generally Not Possible to Do , But we have different methods to Achieve this here the methods check once
Probably a few methods would work.
1) Serialize the String[] in JSON
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) Come up with a delimiter that never appears in the strings
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);
And then rebuild it like:
myValues = str.Split(new string[] { "|||" }, StringSplitOptions.RemoveEmptyEntries);
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.
I have a byte array. I need to assign it to a hidden filed and retrieve the result.How to achieve this?
You'll need to store it in the hidden field as a string, so you could do:
hiddenField.Value = Convert.ToBase64String(data);
And then convert it back later:
byte[] data = Convert.FromBase64String(hiddenField.Value);
It would be a bit more thorough if you could provide an example of the data, too.
But It was Generally Not Possible to Do , But we have different methods to Achieve this here the methods check once
Previous Post I was Explained about the Fileupload show selected file in label when file selected , Show jQuery UI Dialog Popup Window from Server Side ( Code Behind ) in ASP.Net , How to use the window.open() method , CONFIRMATION BOX WITH YES NO BUTTONS USING AJAX MODAL POPUP EXTENDER IN ASP.NET , Dynamic Theme Switching - Creating User-Selectable Themes
Probably a few methods would work.
1) Serialize the String[] in JSON
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) Come up with a delimiter that never appears in the strings
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);
And then rebuild it like:
myValues = str.Split(new string[] { "|||" }, StringSplitOptions.RemoveEmptyEntries);
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.
I have a byte array. I need to assign it to a hidden filed and retrieve the result.How to achieve this?
You'll need to store it in the hidden field as a string, so you could do:
hiddenField.Value = Convert.ToBase64String(data);
And then convert it back later:
byte[] data = Convert.FromBase64String(hiddenField.Value);
It would be a bit more thorough if you could provide an example of the data, too.
Comments
Post a Comment