Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now

Asp.net show alert message from code behind using javascript

Asp.net show alert message from code behind using javascript

In this article we will learn how to call JavaScript function and show JavaScript alert message from code behind using asp.net.
To display JavaScript alert message from code behind we can do by using ScriptManager.RegisterStartupScript() method. This method will allow us to show alert message direclty from code behind or we can call javascript function from code behind in asp.net.

Declaration of ScriptManager.RegisterStartupScript() metod:

ScriptManager.RegisterStartupScript(Control control, Type type,string key, string script,
bool addScriptTags)


Here I will explain how to show JavaScript alert message in code behind and how to call JavaScript function from code behind in asp.net. For that first create new website and write following code in aspx page

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Call Javascript functions and show alert message from code behind file in asp.net Page</title>
<script type="text/javascript">
function Showalert() {
alert('Call JavaScript function from codebehind');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnSubmit" runat="server" Text="Show alert" onclick="btnSubmit_Click" />
<asp:Button ID="btnClick" runat="server" Text="Call JavaScript Function"
onclick="btnClick_Click" />
</div>
</form>
</body>
</html>

using System;
using System.Web.UI;



After that write the following code in code behind

C# code

protected void Page_Load(object sender, EventArgs e)
{
}
//This button click event is used to show alert message directly
protected void btnSubmit_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this,GetType(),"showalert","alert('Only alert Message');",true);
}
// This button click event is used to show alert message from JavaScript function
protected void btnClick_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);
}




TripleDES Encryption In C# And .NET Core



DES is a symmetric key encryption algorithm.   Same key is being used for encryption and decryption.  So challenge in using symmetric key algorithm is that we need to have the same key for decryption which is used for encryption. People follow different approach to save key. Either they append key with cryptic text or physically save it somewhere.  I am going to ask user to input some string as key. I will calculate MD5 hash on that string input by user to make key. Then I will use this key to encrypt and decrypt the plain text.

using System; 
using System.IO; 
using System.Security.Cryptography; 
class TripleDESExample
    public static void Main() { 
        Console.WriteLine("Enter data that needs to be encrypted..."); 
        string data = Console.ReadLine(); 
        Apply3DES(data); 
        Console.ReadLine(); 
    } 
    static void Apply3DES(string raw) { 
        try
            // Create 3DES that generates a new key and initialization vector (IV). 
            // Same key must be used in encryption and decryption 
            using(TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider()) { 
                // Encrypt string 
                byte[] encrypted = Encrypt(raw, tdes.Key, tdes.IV); 
                // Print encrypted string 
                Console.WriteLine("Encrypted data:" + System.Text.Encoding.UTF8.GetString(encrypted)); 
                // Decrypt the bytes to a string. 
                string decrypted = Decrypt(encrypted, tdes.Key, tdes.IV); 
                // Print decrypted string. It should be same as raw data 
                Console.WriteLine("Decrypted data: " + decrypted); 
            } 
        } catch (Exception exp) { 
            Console.WriteLine(exp.Message); 
        } 
        Console.ReadKey(); 
    } 
    static byte[] Encrypt(string plainText, byte[] Key, byte[] IV) { 
        byte[] encrypted; 
        // Create a new TripleDESCryptoServiceProvider. 
        using(TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider()) { 
            // Create encryptor 
            ICryptoTransform encryptor = tdes.CreateEncryptor(Key, IV); 
            // Create MemoryStream 
            using(MemoryStream ms = new MemoryStream()) { 
                // Create crypto stream using the CryptoStream class. This class is the key to encryption 
                // and encrypts and decrypts data from any given stream. In this case, we will pass a memory stream 
                // to encrypt 
                using(CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) { 
                    // Create StreamWriter and write data to a stream 
                    using(StreamWriter sw = new StreamWriter(cs)) 
                    sw.Write(plainText); 
                    encrypted = ms.ToArray(); 
                } 
            } 
        } 
        // Return encrypted data 
        return encrypted; 
    } 
    static string Decrypt(byte[] cipherText, byte[] Key, byte[] IV) { 
        string plaintext = null
        // Create TripleDESCryptoServiceProvider 
        using(TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider()) { 
            // Create a decryptor 
            ICryptoTransform decryptor = tdes.CreateDecryptor(Key, IV); 
            // Create the streams used for decryption. 
            using(MemoryStream ms = new MemoryStream(cipherText)) { 
                // Create crypto stream 
                using(CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read)) { 
                    // Read crypto stream 
                    using(StreamReader reader = new StreamReader(cs)) 
                    plaintext = reader.ReadToEnd(); 
                } 
            } 
        } 
        return plaintext; 
    } 
    private static void EncryptFile(String inName, String outName, byte[] desKey, byte[] desIV) { 
        //Create the file streams to handle the input and output files. 
        FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read); 
        FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write); 
        fout.SetLength(0); 
        //Create variables to help with read and write. 
        byte[] bin = new byte[100]; //This is intermediate storage for the encryption. 
        long rdlen = 0; //This is the total number of bytes written. 
        long totlen = fin.Length; //This is the total length of the input file. 
        int len; //This is the number of bytes to be written at a time. 
        DES des = new DESCryptoServiceProvider(); 
        CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write); 
        Console.WriteLine("Encrypting..."); 
        //Read from the input file, then encrypt and write to the output file. 
        while (rdlen < totlen) { 
            len = fin.Read(bin, 0, 100); 
            encStream.Write(bin, 0, len); 
            rdlen = rdlen + len; 
            Console.WriteLine("{0} bytes processed", rdlen); 
        } 
        encStream.Close(); 
        fout.Close(); 
        fin.Close(); 
    } 
}  

Output is like following 

Exclusive Clothing Sale On Soft & Soothe

Get up to 63% off on our latest collection.

Shop Now