How do disable back button in browser

Generally there are cases when we want to disable back button in browser. We have multiple options to disable back button in browser. Here are the few:-

Java Script Way
We can use following java script to prevent back button:-
<script type = "text/javascript" >
function disableBackButton()
setTimeout("disableBackButton()", 0);

We can also add Meta Tags in Head Section like this:-
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />

If you want to do above approach through code. Here is the trick:-

HtmlMeta pragma = new HtmlMeta();

pragma.HttpEquiv = "Pragma";

pragma.Content = "no-cache";



HtmlMeta expires = new HtmlMeta();

expires.HttpEquiv = "Expires";

expires.Content = "-1";


ASP.Net Way
In ASP.Net we can use Cache to disable back button like this:-





A potentially dangerous Request.Form value was detected from the client

Above error comes because by default ASP.Net blocks the possible Script Injection request. e.g. if user type <script>alert('yes');</script> in your text box and when you display the text in text box, this java script will be executed instead of displaying text.

To resolve this problem you need to keep few things in mind:-

1- We can remove HTML Tags from TextBoxes with a simple regular expression filter like this:-
<asp:textbox id="txtSecureTextBox" runat="server" onblur="this.value = this.value.replace(/&lt;\/?[^>]+>/gi, '');">

2- Add ValidateRequest="false" in your Page directive line incase you want to disable this on single page.
<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false"/>

3- Incase you want to disable validation through out the site then you can do it in your web.config file like this:-
    <pages validateRequest="false" />  

4- Use HTMLEncode whenever you are displaying unsafe contents in labels like this:-
Label1.Text = Server.HtmlEncode(TextBox1.Text) 

For further details see Request Validation - Preventing Script Attacks.

Generate High quality Thumbnail Images and Maintain Aspect Ratio

I was looking up for a simple thumbnail generator through which I can generate thumbnail images. I found some code and tried to implement that but thumb images look too ugly and stretched in most of the cases. Solution to good looking thumbnail is that we have to maintain aspect ratio of actual image in our thumbnail image otherwise thumbnail will look really bad. Here is the class through which you can easily generate high quality thumbnails.

using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace ConsoleApplication1
    public class ThumbnailGenerator
        /// <summary>
        /// Generates the thumbnail of given image.
        /// </summary>
        /// <param name="actualImagePath">The actual image path.</param>
        /// <param name="thumbnailPath">The thumbnail path.</param>
        /// <param name="thumbWidth">Width of the thumb.</param>
        /// <param name="thumbHeight">Height of the thumb.</param>
        public static void Generate(string actualImagePath, string thumbnailPath, int thumbWidth, int thumbHeight)
            Image orignalImage = Image.FromFile(actualImagePath);
            // Rotating image 360 degrees to discart internal thumbnail image
            // Here is the basic formula to mantain aspect ratio
            // thumbHeight   imageHeight     
            // ----------- = -----------   
            // thumbWidth    imageWidth 
            // Now lets assume that image width is greater and height is less and calculate the new height
            // So as per formula given above
            int newHeight = orignalImage.Height * thumbWidth / orignalImage.Width;
            int newWidth = thumbWidth;
            // New height is greater than our thumbHeight so we need to keep height fixed and calculate the width accordingly
            if (newHeight > thumbHeight)
                newWidth = orignalImage.Width * thumbHeight / orignalImage.Height;
                newHeight = thumbHeight;
            //Generate a thumbnail image
            Image thumbImage = orignalImage.GetThumbnailImage(newWidth, newHeight, null, IntPtr.Zero);
            // Save resized picture
            var qualityEncoder = System.Drawing.Imaging.Encoder.Quality;
            var quality = (long)100; //Image Quality 
            var ratio = new EncoderParameter(qualityEncoder, quality);
            var codecParams = new EncoderParameters(1);
            codecParams.Param[0] = ratio;
            //Right now I am saving JPEG only you can choose other formats as well
            var codecInfo = GetEncoder(ImageFormat.Jpeg);
            thumbImage.Save(thumbnailPath, codecInfo, codecParams);
            // Dispose unnecessory objects
        /// <summary>
        /// Gets the encoder for particulat image format.
        /// </summary>
        /// <param name="format">Image format</param>
        /// <returns></returns>
        private static ImageCodecInfo GetEncoder(ImageFormat format)
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
                if (codec.FormatID == format.Guid)
                    return codec;
            return null;

To use this class simply call Generate function of ThumbnailGenerator class. Here is a sample:-
ThumbnailGenerator.Generate(@"C:\images\myPicture.jpg", @"C:\images\myPictureThumb.jpg", 100, 150);