Solved

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

Posted on 2009-05-13
3
619 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

696 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