C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Encryption »

ViewState Compression in web page


Posted Date: 16 Sep 2008    Resource Type: Code Snippets    Category: Encryption
Author: Shivashankar ChincholiMember Level: Gold    
Rating: 1 out of 5Points: 10



This code shows how to compress large viewstate data in asp.net page

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;

using Shivu;

public partial class ViewStateCompression : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
using (SqlConnection connection = new SqlConnection("server=shivu;database=NewAgencyProspect;uid=sa;pwd=ntplupl"))
{
connection.Open();
using (SqlDataAdapter dataAdapter = new SqlDataAdapter("select * from AgencyProspects", connection))
{
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
ViewState["dataSet"] = dataSet.Tables[0].Rows.Count.ToString();
}
}
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
}

protected override object LoadPageStateFromPersistenceMedium()
{
LosFormatter formatter = new LosFormatter();

string viewState = Request.Form["__VSTATE"];
byte[] bytes = Convert.FromBase64String(viewState);
bytes = Compressor.Decompress(bytes);

return formatter.Deserialize(Convert.ToBase64String(bytes));
}

protected override void SavePageStateToPersistenceMedium(object viewState)
{
try
{
LosFormatter formatter = new LosFormatter();
StringWriter writer = new StringWriter();
formatter.Serialize(writer, viewState);
string viewStateString = writer.ToString();
byte[] bytes = Convert.FromBase64String(viewStateString);
bytes = Compressor.Compress(bytes);
ClientScript.RegisterHiddenField("__VSTATE", Convert.ToBase64String(bytes));
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
}

protected void Button1_Click(object sender, EventArgs e)
{
string count = (string)ViewState["dataSet"];
}
}

//Class

using System.IO;
using System.IO.Compression;
using System;
using System.Diagnostics;

/// <summary>
/// Summary description for Compressor
/// </summary>
///
namespace Shivu
{
public class Compressor
{
public static byte[] Compress(byte[] data)
{
MemoryStream output = new MemoryStream();
try
{
GZipStream gzip = new GZipStream(output,
CompressionMode.Compress, true);
gzip.Write(data, 0, data.Length);
gzip.Close();
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
return output.ToArray();
}

public static byte[] Decompress(byte[] data)
{
MemoryStream output = new MemoryStream();
try
{
MemoryStream input = new MemoryStream();
input.Write(data, 0, data.Length);
input.Position = 0;
GZipStream gzip = new GZipStream(input,
CompressionMode.Decompress, true);

byte[] buff = new byte[64];
int read = -1;
read = gzip.Read(buff, 0, buff.Length);
while (read > 0)
{
output.Write(buff, 0, read);
read = gzip.Read(buff, 0, buff.Length);
}
gzip.Close();
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
return output.ToArray();
}

}
}




Responses

Author: Venkat    17 Sep 2008Member Level: Silver   Points : 0
Nice article.But i cant get anything.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Web page  .  ViewState  .  Compression  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Encryption of string in C#
Previous Resource: Password Encryption using C#
Return to Discussion Resource Index
Post New Resource
Category: Encryption


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use