Adding JavaScript To Many Pages(More than 500) in asp.net without MasterPage

136 Views Asked by At

I am working on Project in which there are more than 500 .aspx pages(Popups uses ShowModelDialog) I want Suppress/Disable Enter Key for each Page

I want to Add Below Code for Every page (all Popup )or for every Request made by User. Unfortunately There is no master page in the project

<script type="text/javascript">

function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
}

document.onkeypress = stopRKey;

</script> 

void Application_BeginRequest(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown
        System.Web.UI.Page mypage = (System.Web.UI.Page)HttpContext.Current.Handler;
        if (mypage != null)
        {
            mypage.ClientScript.RegisterClientScriptBlock(GetType(), "MyScriptKey", "alert('hi')", true);
        }

    }

I try to add this script in Global.asax in Application_BeginRequest but no luck

2

There are 2 best solutions below

0
On BEST ANSWER

I used Application_PreRequestHandlerExecute event in Global.asax file and it worked.

void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs
        HttpContext context = ((HttpApplication)sender).Context;
        Page mypage = context.CurrentHandler as Page;
        if (mypage != null)
        {

            mypage.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), @"function stopRKey(evt) {
          var evt = (evt) ? evt : ((event) ? event : null);
          var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
          if ((evt.keyCode == 13) && (node.type=='text'))  {return false;}
           }
            document.onkeypress = stopRKey; ", true);


            mypage.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), @"function validatedot(val, evt) {
            var theEvent = evt || window.event;
            var key = theEvent.keyCode || theEvent.which;
            key = String.fromCharCode(key);

            y = val.value.split(/\./);

            var regex = /[0-9]|\./;

            if (y.length > 1 && key == '.') {

                theEvent.returnValue = false;
                if (theEvent.preventDefault) theEvent.preventDefault();
            }
            if (!regex.test(key)) {

                if (theEvent.keyCode != 8 && theEvent.keyCode != 9) {
                    //alert('Enter only number');
                    //alert(theEvent.keyCode);
                    theEvent.returnValue = false;
                    if (theEvent.preventDefault) theEvent.preventDefault();
                }
            }


        }", true);
        }

    }
0
On

If you need to edit a large number of files, how to do it strongly depends on which platform you're working on and what tools do you have at your disposal.

On Windows, I would use Notepad++. With it you can do a search and replace on multiple files on a subdirectory, filtering only certain types of files (*.asp or *.php) and using regular expressions to search and modify text:

CTRL-F to open the find dialog. Go to "Find in files" tab. Select Directory and filters as appropriate. Select "Search Mode" = "Regular Expression" and use

(</body>)

for "Find What" field and

***YOUR SCRIPT IN A SINGLE LINE HERE*** \r\n \1

for "Replace with" field.

Note the "\1" that is used to place the closing body tag again in the file (otherwise it would be overwritten).

On unix and mac, you could probably use some equivalent text editors with PCRE regular expression editing functions but I'm afraid I cannot help you there.