Click here to Skip to main content
15,886,362 members
Articles / Web Development / ASP.NET

Post Serialized Objects (Data) in ASP.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Feb 2013CPOL2 min read 23.1K   143   5  
This is helper class to post serialized objects (Data) to other page in asp.net.
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using TripleDESEncryption;
namespace WebUtilities
{
    public partial class PostDataHelper
    {
        [Serializable]
        public class PostData : IEnumerable
        {
            private NameValueCollection values = new NameValueCollection();
            /// <summary>
            /// FormName
            /// </summary>
            public string FormName
            {
                get
                {
                    return this.Get("__TransferData").ToString();
                }
                set
                {
                    this.Add("__TransferData", value);
                }
            }
            /// <summary>
            /// Adds object to data collection
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            public void Add(string key, object value)
            {
                if (value != null && value.GetType().IsSerializable)
                {
                    LosFormatter x = new LosFormatter();
                    StringWriter sw = new StringWriter();
                    x.Serialize(sw, value);
                    this.values.Set(key, sw.ToString());
                    return;
                }
                throw new ArgumentException("Invalid object for PostData", "value");
            }
            /// <summary>
            /// Get object from data collection
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public object Get(string key)
            {
                string tempValue = this.values[key];
                object result;
                if (tempValue == null)
                {
                    result = null;
                }
                else
                {
                    LosFormatter x = new LosFormatter();
                    StringReader sw = new StringReader(tempValue);
                    result = x.Deserialize(sw);
                }
                return result;
            }
            /// <summary>
            /// ctor
            /// </summary>
            public PostData()
            {
                this.FormName = "DefaultForm";
            }
            /// <summary>
            /// Gets serialized value, i.e.string
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            internal string GetRawData(string key)
            {
                return this.values[key];
            }
            /// <summary>
            /// Sets serialized value
            /// </summary>
            /// <param name="key"></param>
            /// <param name="value"></param>
            internal void SetRawData(string key, string value)
            {
                this.values[key] = value;
            }
            /// <summary>
            /// Clears all data except FormName
            /// </summary>
            public void Clear()
            {
                string fname = this.FormName;
                this.values.Clear();
                this.FormName = fname;
            }
            /// <summary>
            /// Checks if any value is present at key
            /// </summary>
            /// <param name="key"></param>
            /// <returns></returns>
            public bool HasObjectForKey(string key)
            {
                return !string.IsNullOrEmpty(this.values.Get(key));
            }
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public IEnumerator GetEnumerator()
            {
                return this.values.GetEnumerator();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer TCS
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions