473,395 Members | 1,670 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to add a Javascript event in codebehind

Hi all,

I know this is a trivial question, but I can't seem to figure it out. Pardon my inarticulation in asking the question...

I'm using ASP.NET such that in the OnLoad event of the codebehind, some javascript is dynamically assigned to a button's "onclick" event. Works as expected if the user clicks on the button. However, I also want this javascript to fire when other events occur on the page (i.e. certain values are entered into select textboxes). How, in the codebehind, can I simulate the user clicking this button so that the onclick javascript fires?

Thanks.

Robert
May 28 '08 #1
4 1665
DrBunchman
979 Expert 512MB
Hi Robert,

You can assign javascript to other controls in the same way as you did with your button. Check out this for a list of the possible events that you can use.

If you want to use the same javascript code for different events then you should store it as a function. A Javascript function defined between the <head /> tags can be called from any part of your page. Simply define the function and call it like so:

On your .aspx page:
Expand|Select|Wrap|Line Numbers
  1. <head> 
  2. <script type="text/javascript">
  3. function TestFunction()
  4. {
  5. alert('Hello World!!')
  6. }
  7. </script>
  8. </head>
  9.  
On your code behind page:
Expand|Select|Wrap|Line Numbers
  1.  
  2. textbox1.attributes.add("onchange", "TestFunction();")
  3.  
Does this help?

Dr B

PS Robert, you've posted your question in the ASP Forum which is for Classic ASP only - I've moved it for you but please post all your future ASP.NET questions in the .NET forum. Thanks.
May 29 '08 #2
deric
92
Could the following help?

In your code behind:
Expand|Select|Wrap|Line Numbers
  1. Public myEvent As String
  2. myEvent = ""
  3. ...
  4. myEvent = "myFunction();"
  5.  
In your .aspx page:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <script language="javascript">
  3. function myFunction(){
  4. ...
  5. }
  6. </script>
  7. ...
  8. <input type="button" name="myButton" onclick="<% =myEvent %>" />
  9. <input type="input" name="myText" onchange="<% =myEvent %>" />
  10. ...
  11. </html>
  12.  
May 29 '08 #3
kunal pawar
297 100+
DrBunchman is gives right code and solution hope this will helps you
May 30 '08 #4
First of all, thanks for moving the thread to the .NET forum instead. I'm still learning...

Second, thanks for the response. I kind of figured that calling the javascript was the way to do it, but I just couldn't get my head wrapped around it.

Thanks again.

Robert
May 30 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Allan M. | last post by:
I have a series of select boxes that must be populated client side, because they interact with each other. The design specification calls for these boxes to be updated without having to make a...
5
by: Vinod Kumar | last post by:
Hi All, Can I access a Javascript function written in an ASP.NET from the codebehind (C#) of that page? If yes, kindly give the code snippet to do this. Thanks and Regards Vinod Kumar
2
by: Alex | last post by:
Hi all, I'm writing a small web application which searches a database based on a date field, and populates a datagrid control with the results. The datagrid control has selection buttons added...
0
by: rahulkate | last post by:
Hello Everybod I have a problem hope you can help me out I have a treeview and on the selectedindexchange event of the treeview I want to execute some code from the vb codebehind file and a...
1
by: Jorge Ponte | last post by:
hi I have a Web User Control (ascx) - lets call it "My_WUC" - in a Web form. In that WUC I want have a textbox and a button. I want to click on the button and open a popup (I use javascript for...
2
by: darrel | last post by:
I have an asp:RadioButtonList. If one of the items is selected, I'd like a text field to appear. I tried using an ONCHANGE event via the codebehind, but couldn't get that to work. Plus, I'm...
5
by: Jimmy | last post by:
Hi I am trying to check whether checkboxes are checked in my user control. I have problems getting the right reference to the checkboxes: <%@ Page language="c#" Codebehind="WebForm2.aspx.cs"...
6
by: simon | last post by:
hello, what code would i use to kick off a javascript script after i had registered it? If (Not Page.IsClientScriptBlockRegistered("jsScript")) Then Page.RegisterClientScriptBlock("jsScript",...
11
by: Hakan ÖRNEK | last post by:
Hi , I register client scripts like this; -------------------------------------------------------------------------- ----------- Public Sub CreateClientAlert(ByVal Message As String, ByVal...
4
by: =?Utf-8?B?UmljaGFyZA==?= | last post by:
Hi, I have an ASP .NET page with codebehind in a vb file. The page has a dropdown control, and a JS function which used HTTP XML Request to get the data from the database, and populate the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.