Pass value from one page to another using Cookies in ASP.Net

Last Reply 2 months ago By dharmendr

Posted 2 months ago

There is an example:

www.aspforums.net/Threads/240052/Pass-data-from-one-page-to-another-page-on-HTML-Anchor-Link-click-in-JavaScript

Here value is passing using sessionStorage, then showing in aspx file. It's useful.

But I want to assign cardno value to string type variable, like:

string cn=1111234567123456;

"cn" will be used in cs code.

 

 

You are viewing reply posted by: dharmendr 2 months ago.
Posted 2 months ago Modified on 2 months ago

Hi Alauddin,

To pass data from one page to another use cookie. Cookie will save the value. Then retrieve the value from cookie in page load and display in label.

Note: This method will work only if Cookies are enabled in browser.

For more details refer below article.

Send (Pass) Data (Values) from one page to another using jQuery

CS.aspx

<asp:Label ID="lblTable" runat="server"></asp:Label>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery.cookie/1.3.1/jquery.cookie.js"></script>
<script type="text/javascript">
    window.onload = function () {
        var table = document.getElementsByTagName("table");
        var trs = table[0].getElementsByTagName('tr');
        for (var i = 0; i < trs.length; i++) {
            var tds = trs[i].getElementsByTagName('td');
            var cardno = tds[1].innerText;
            if (cardno.length > 5) {
                var firstFour = cardno.substr(0, 4);
                var lastFour = cardno.substr(cardno.length - 4, cardno.length);
                var middle = cardno.substr(4, cardno.length - 8).replace(/[0-9]/g, "*");
                var newLink = document.createElement("a");
                newLink.setAttribute("href", "Default.aspx");
                newLink.setAttribute("onclick", 'return CardNumber("' + cardno + '")');
                text = document.createTextNode(firstFour + middle + lastFour);
                newLink.appendChild(text);
                tds[1].innerHTML = "";
                tds[1].appendChild(newLink);
            }
        }
    }
    function CardNumber(cardno) {
        $.cookie("cardNumber", cardno);
    }         
</script>

CS.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("<table id='01717458720_CardList'>");
        sb.Append("<tr>");
        sb.Append("<td id='product_type'>CREDIT CARD</td>");
        sb.Append("<td id='card_no' abbr='*'>1111234567123456</td>");
        sb.Append("<td id='card_status'>INACTIVE</td>");
        sb.Append("<td id='acct_no'>N/A</td>");
        sb.Append("<td id='name'>Mr. X</td>");
        sb.Append("</tr><tr>");
        sb.Append("<td id='product_type'>DEBIT CARD</td>");
        sb.Append("<td id='card_no'>0000000000000000</td>");
        sb.Append("<td id='card_status'>ACTIVE</td>");
        sb.Append("<td id='acct_no'>55555555555</td>");
        sb.Append("<td id='name'>Mrs. Y</td>");
        sb.Append("</tr></table>");
        lblTable.Text = sb.ToString();
    }
}

Default.aspx

<asp:Label ID="lblCardNo" runat="server" />

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        lblCardNo.Text = Request.Cookies["cardNumber"].Value;
    }
}

Screenshot