Open Custom Help File on F1 press

Hello readers,
This is new topic for which i have written a code. When we press F1 key then its opens the help for that document and same when we a running any web application or internet and if we press F1 key then it opens the browser help. So here is the scenario, I want to open my own created help file on pressing F1 key. The code is not much complicated. Just we have to trap the F1 key code and cancel its default function and perform the action to open my own created help file that is a word document file. We have to use JavaScript to trap F1 event. After that we retrieve the page URL and its name and open a new popup window by passing file name in query string. To make it more simple to identify which file has to be opened for the current page, I named the help files as the page name. for example if page name is abc.aspx then its help file name is abc.doc.

ASPX Page Code  :

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


<!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 runat="server">
    <title>Open Help File</title>
    <script type="text/javascript" language="javascript">

        document.onkeydown = function (e)
        {
            if (navigator.appName != "Microsoft Internet Explorer")
            {
                var evt = (window.event) ? event : e;
                var code = evt.keyCode;             

                if (code == 112)
                {
                    var url = document.URL.split('/');
                    var pagename = url[url.length - 1];
                    window.open(window.location.protocol + "//" + window.location.host + "/help/webpages/Help.aspx?val=" + url[url.length - 1], "Help");
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        document.onhelp = function ()
        {
            var url = document.URL.split('/');
            var pagename = url[url.length - 1];
            window.open(window.location.protocol + "//" + window.location.host + "/help/webpages/Help.aspx?val=" + url[url.length - 1], "Help");
            return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    </form>
</body>
</html>

 Now add another page and give it some suitable name like help.aspx or any thing you like and the Code Behind Page means in the .cs page (help.aspx.cs) write the below written code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class webpages_Help : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["val"] != null)
        { 
            try
            {
                string helpfor = Request.QueryString["val"].ToString().Replace("aspx", "doc").ToLower();
                if (File.Exists(Server.MapPath("~/webpages/helpfiles/" + helpfor.ToLower())))
                {
                    Response.Clear();
                    Response.Buffer = true;
                    Response.AddHeader("Content-Disposition", "attachment; filename=IFMS Help For " + helpfor.Replace(".doc", ""));
                    Response.ContentType = "Application/msword";
                    Response.TransmitFile(Server.MapPath("~/webpages/helpfiles/" + helpfor.ToLower()));
                    Response.End();
                }
                else
                {
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), new Guid().ToString(), "alert('Help for this page is not avaliable currently.\\r\\nSorry for inconvenience.'); window.close();", true);
                }
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), new Guid().ToString(), "alert('Unable to process your request.\\r\\nSorry for inconvenience.');");
            }

        }
    }
}

Now create a folder under your application names helpfiles and add some document files having name same as the aspx pages for which you want to add help file. Your directory structure should look like as in picture below:-




now run your application and press F1.... and help file open... :)

Run each page separately except help.aspx

Download the complete code from the link below:-

https://docs.google.com/open?id=0B2iokvd3EpXAZjUyMWMzOTAtYjYxYi00ZDllLWI4ZjUtMTI0OWFlNmMyMmI1

Comments

Popular posts from this blog

Get Query String Values With Javascript

Change Css Class of li in ul onclick

Change Text Color with Javascript