I was working on a scenario where the responsibility was to implement Product Comparison functionality.There was a requirement where I want to persisting info (product Id from different category page) across multiple pages using javascript. Then use those product ids in Product Comparison page.Each Product Category page shows multiple products, with a check box associated with each product, which allow user to select products to compare. As per requirement user can select products from diferent category and when click compare product button ,then all selected products from diferent page must be go to compare Product page.So I need to persist Product id in session.
1. To implement this functionality I have used one Hidden input control to get data from cookie on load event.
<input type="hidden" id="HiddenVar" runat="server" enableviewstate="true" />
2. To Collect all selected product Id, I have written a javascript function and call it on onClick event of CheckBox.
checkbox.Attributes.Add("onClick", "updateProductIdInArray(this)");
Here I have used a global variable as array to update selected Product ids .As a result whenever a user check or uncheck the check box control, the array object will be updated ( add or remove product id from array object) .Then update the value of hidden control by getting all ids from array object (you can write a separate function to get all product id from array and make a text separated by comma ) .
3. Write a function to save the info in a Session and call the function when user click on button or link to go to different page.The idea is before go to the specified page the javascript function will save the data from Hidden control value into a Session.
Control.Attribute.Add("onclick", "javascript:SaveSelectedProductId(););
Here I am using XmlHttprequest object to send a asyncronyous request to server by passing a Temp.aspx page url .The url must have a querystring and the value will be the data you want to save in session.The main idea behind this to save the value in session when the requested page will render.e.x("/Pages/Temp.aspx?hiddenValue=" + dataTosave").
//Save Product
Ids in Session
function SaveSelectedProductId() {
var
MyHiddenControl = document.getElementById('<%=
myHiddenVar.ClientID %>');
var
dataTosave = MyHiddenControl.value;
var
finalUrl = "/Pages/Temp.aspx?hiddenValue="
+ dataTosave;
var
xmlhttp = false;
try
{
xmlhttp = new
ActiveXObject("Msxml2.XMLHTTP"); /* for IE < 5 */
}
catch
(e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch
(E)

Join the conversation! Your thoughts help the community grow.