Sunday 22 February 2015

jQuery Convert JSON Data to HTML Table using jQuery Row Append Properties

In this article ,I am explain how to convert JSON data to table form using jQuery. Many of time we got data in JSON form by using many APIs,Web Services, Web Methods etc. So here I try to solve that.

Designer Source Code :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JSONDatatoHTML.aspx.cs" Inherits="JSONDatatoHTML" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>convert json data to html table using jquery</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    <script type="text/javascript">
        $(function () {
            var myList = [{ "Name": "Jitendra Gangwar", "Designation": "Software Developer", "Location": "Delhi" },
                          { "Name": "Suraj", "Designation": "Software Developer", "Location": "Delhi" },
                          { "Name": "Arun", "Designation": "Software Developer", "Location": "Lucknow" },
                          { "Name": "Gaytri Mishra", "Designation": "HR", "Location": "Delhi" },
                          { "Name": "Dhirendra", "Designation": "Designer", "Location": "Lucknow" },
                          { "Name": "Sateesh", "Designation": "Software Developer", "Location": "Ghaziabad" },
                          { "Name": "Praveen", "Designation": "Manager", "Location": "Delhi"}]
            Bindhtmltable(myList);
        })
        function Bindhtmltable(list) {
            var cols = addheadercols(list);
            for (var i = 0; i < list.length; i++) {
                var row = $('<tr/>');
                for (var colIndex = 0; colIndex < cols.length; colIndex++) {
                    var cellValue = list[i][cols[colIndex]];
                    if (cellValue == null) { cellValue = ""; }
                    row.append($('<td/>').html(cellValue));
                }
                $("#htmltable").append(row);
            }
        }

        function addheadercols(list) {
            var colset = [];
            var headerTr = $('<tr/>');
            for (var i = 0; i < list.length; i++) {
                var rows = list[i];
                for (var key in rows) {
                    if ($.inArray(key, colset) == -1) {
                        colset.push(key);
                        headerTr.append($('<th/>').html(key));
                    }
                }
            }
            $("#htmltable").append(headerTr);
            return colset;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table id="htmltable" border="2" cellpadding="5">
        </table>
    </div>
    </form>
</body>
</html>

OutPut:



Wednesday 4 February 2015

Encrypt Decrypt Cookies in Asp.net

In this article, I am explain how to Encrypt and Decrypt cookie values. 

Design Source Code:

<div>
            <asp:TextBox ID="txtvalue" runat="server" placeholder="Enter Some Text" Width="250">
            </asp:TextBox><br />
            <asp:Label runat="server" ID="lblmsg" ForeColor="Green" Font-Bold="true"></asp:Label><br />
            <asp:Button ID="btnEncrypt"
                runat="server" Text="Encrypt" OnClick="btnEncrypt_Click" />&nbsp;&nbsp;
            <asp:Button ID="btnDecrypt" runat="server" Text="Decrypt" OnClick="btnDecrypt_Click" Style="height: 26px" />
        </div>

Code Behind:
Using namespaces:
using System.Text;
using System.Web.Security;

protected void btnEncrypt_Click(object sender, EventArgs e)
{
byte[] bCookieText = Encoding.UTF8.GetBytes(txtvalue.Text);
string sEncryptedValue = Convert.ToBase64String(MachineKey.Protect(bCookieText, "ProtectCookie"));

HttpCookie oHttpCookie = new HttpCookie("NameOfCookie", sEncryptedValue);

//---- Set expiry time of cookie.
oHttpCookie.Expires.AddDays(5);
Response.Cookies.Add(oHttpCookie);
lblmsg.Text = sEncryptedValue;
}
protected void btnDecrypt_Click(object sender, EventArgs e)
{
byte[] bytes = Convert.FromBase64String(Request.Cookies["NameOfCookie"].Value);
byte[] bOutput = MachineKey.Unprotect(bytes, "ProtectCookie");
string sOutput = Encoding.UTF8.GetString(bOutput);
lblmsg.Text = sOutput;

}

For Asp.Net 4.0

Encryption:
var plaintextBytes = Encoding.UTF8.GetBytes("Jitendra Gangwar");
var encryptedValue = MachineKey.Encode(plaintextBytes, MachineKeyProtection.All);
Response.Write(encryptedValue.ToString());

Decryption:
var decryptedBytes = MachineKey.Decode(encryptedValue, MachineKeyProtection.All);
var decryptedValue = Encoding.UTF8.GetString(decryptedBytes);
Response.Write(decryptedValue);

OutPut:




Download Here