Solved

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

Posted on 2009-05-13
3
609 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
Comment Utility
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
Comment Utility
Thanks for your response.  I appreciate it.

0
 

Author Closing Comment

by:pb102674
Comment Utility
Thanks for your help.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

12 Experts available now in Live!

Get 1:1 Help Now