Solved

How to register multiple JavaScript functions on a textbox in ASP.Net?

Posted on 2009-05-13
3
612 Views
Last Modified: 2013-11-07
How to register multiple JavaScript functions running on a textbox in ASP.Net?
Bookmark:
Question: Im using a Multiview control having six views. Each of the views has about 10 textboxes and from these 10 checkboxes, there are about 6 textboxes having JavaScript code for events such as onkeypress, onkeyup, onblur, oncopy, onpaste. Right now Im calling all the JavaScript functions from code-behind in Page_OnLoad as:
        Txtbox1.Attributes.Add(onkeypress, javascript: Function1();;
      Txtbox1.Attributes.Add(onkeyup, javascript: Function2();;
      Txtbox1.Attributes.Add(onblur, javascript: Function3();;
       Txtbox1.Attributes.Add(oncopy, return false;);
       Txtbox1.Attributes.Add(onpaste, return false;);

      Txtbox2.Attributes.Add(onkeypress, javascript: Function1();
      Txtbox2.Attributes.Add(onkeyup, javascript: Function2();
      Txtbox2.Attributes.Add(onblur, javascript: Function3();
       Txtbox2.Attributes.Add(oncopy, return false;);
      Txtbox2.Attributes.Add(onpaste, return false;);
'
'
'
'
'
'

      Txtbox6.Attributes.Add(onkeypress, javascript: Function1();
      Txtbox6.Attributes.Add(onkeyup, javascript: Function2();
      Txtbox6.Attributes.Add(onblur, javascript: Function3();
       Txtbox6.Attributes.Add(oncopy, return false;);
       Txtbox6.Attributes.Add(onpaste, return false;);

Although this code works without any errors, the code looks so cluttered. So, Im just wondering if theres any other approach I can use to make my code look a lot cleaner than and not as repetitive as shown above for the 6 textboxes. Please help.
0
Comment
Question by:pb102674
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
GuitarRich earned 500 total points
ID: 24373589
if all the functions are the same you could create a method to add the javascript to a textbox parameter that is passed in like this:

private void AddJSToTextBox(TextBox txt)

{

      txt.Attributes.Add("onkeypress", "javascript: Function1();");

      txt.Attributes.Add("onkeyup", "javascript: Function2();");

      txt.Attributes.Add("onblur", "javascript: Function3();");

      txt.Attributes.Add("oncopy" , "return false;");

      txt.Attributes.Add("onpaste", "return false;");

}
 

// then call it like this:

AddJSToTextBox(Txtbox1);

AddJSToTextBox(Txtbox2);

AddJSToTextBox(Txtbox3);

......

AddJSToTextBox(Txtbox6);

Open in new window

0
 

Author Comment

by:pb102674
ID: 24374375
Thanks for your response.  I appreciate it.

0
 

Author Closing Comment

by:pb102674
ID: 31580907
Thanks for your help.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now