Sunday, March 11, 2012

Is it possible to store data on the client (browser)?

You can store it in a client-side javascript array. Ajax is (primarily) for communicating asynchronously with the server.


You can certainly store it cookie, but it will not ensure that the values will be always available upon the next user visit. In that case you can store it using the Asp.net Profile Provider and load it by the Profile Service. Check out this link how to enable profile service for Ajax Framework.

http://ajax.asp.net/docs/tutorials/UsingProfileInformationTutorial.aspx


In The Name of God

hi

for saving and retriving the cookie content in the form of an arraylist you can close your eyes and use these three methods that I have written and used many times:

Saving:

----------

Page: The page that you use these methods in : if you use these methods inline so pass 'this' to the Page arg.

al_Items: the array that you want to save in the cookie.

str_CookieName: obvious

str_CookieContentKey : because you can save different elements in a single cookie so you must pass this name

str_Itemseperator: the character that you use to seperate the array elements so differentiate them from each other

public static boolSave(Page page, ArrayList al_Items, string str_CookieName,string str_CookieContentKey, string str_ItemSeperator)
{

HttpCookie cook;

if (page.Request.Cookies[str_CookieName] != null)
{

cook = page.Request.Cookies[str_CookieName];

page.Response.Cookies[str_CookieName].Expires = DateTime.Now;

}

string str_CookieContent = "";

foreach (object element in al_Items)

str_CookieContent += element.ToString() + str_ItemSeperator;

if (str_CookieContent.EndsWith(str_ItemSeperator))

str_CookieContent = str_CookieContent.TrimEnd(Convert.ToChar(str_ItemSeperator));

cook = new HttpCookie(str_CookieName);

cook[str_CookieContentKey] = str_CookieContent;

cook.Expires = DateTime.MaxValue;

page.Response.Cookies.Add(cook);

return true;

}

-----------

Retrieving:

-----------


public static ArrayListRetrieve(Page page, string str_CookieName,string str_CookieContentKey,string str_ItemSeperator)
{

ArrayList al_Items = new ArrayList();

if (page.Request.Cookies[str_CookieName] != null && page.Request.Cookies[str_CookieName][str_CookieContentKey]!=null)
{
string[] CookieContentElements = page.Request.Cookies[str_CookieName][str_CookieContentKey].Split(new char[] { Convert.ToChar(str_ItemSeperator) }, StringSplitOptions.RemoveEmptyEntries);

foreach (string element in CookieContentElements)

al_Items.Add(element);

}

return al_Items;
}

------------

hope this helps

No comments:

Post a Comment