Solved

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

Posted on 2009-05-13
3
615 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 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