473,499 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Execute a JavaScript Function from .aspx page

83 New Member
Hii all ,

desperately looking for help.

i want to execute one java script function,passvalues(), which is there in my master page.

After doing so much of additions , subtraction and type conversions in the button click event im obataining the values for 3 variables which i need to pass these values to the existing javascript function...how can i pass these values to the javascript function which is thr in master page ..in the end of the button_click event handler..

Expand|Select|Wrap|Line Numbers
  1. Protected void Button_Click(sender object,EventArgs e)
  2. {
  3.   // Type conversions..
  4. //getting the references of other control..
  5. blah  blah  blah
  6.  
  7. value1="";
  8. value2="";
  9. value3="";
  10.  
  11. /*Now i want to pass these values to a javascript function which is there in my master page..based on these values only all my remaining functions work.*/
  12. }
Requesting you all Kindly suggest me the solution .

Is Clientmanager.RegisterStartupScript will work for me ...if yes kindly tell me how can i use tht .

Thanks in advance..
Rgds,
BTR.
Feb 26 '09 #1
7 10636
Dormilich
8,658 Recognized Expert Moderator Expert
to me it looks more like Java....
Feb 26 '09 #2
btreddy
83 New Member
hey NO ,im using ASP.NET and C#.
Feb 26 '09 #3
Dormilich
8,658 Recognized Expert Moderator Expert
well, then it is C#. if this is a problem in C# programming, maybe it's better to ask in the C#-forum.
Feb 26 '09 #4
btreddy
83 New Member
thts not a JavaScript Function..thts an EventHandler in C#..for understanding purpose only i mentioned it there.

leave any syntax errors if you find..but plz understand the problem and suggest me the solution.

Thank you..
Rgds.,
BTR.
Feb 26 '09 #5
acoder
16,027 Recognized Expert Moderator MVP
Since no-one here has much experience with ASP.NET and C#, we can only show you how to use JavaScript in a normal HTML page.

The basic idea is that you need to generate the JavaScript code using the server-side language. How you do that would differ from language to language.
Feb 26 '09 #6
Frinavale
9,735 Recognized Expert Moderator Expert
I see no JavaScript posted above.

I'm a little confused as to what you're trying to do...

Are you trying call a JavaScript function when you click an ASP.NET button?

You need to add the JavaScript to the page (you are doing this by using the RegisterStartupScript method). This will make the JavaScript function available to be used client side (in the web browser).

You cannot use JavaScript code in your C# code....simply because JavaScript code is executed client side (in the web browser) and your C# code is executed on the server.

So, in order to use the JavaScript function, you need to call the JavaScript code during the JavaScript "onclick" event....not in the C# OnClick event.

To do this you add an attribute to the button:
Expand|Select|Wrap|Line Numbers
  1. myButton.Attributes.Add("onclick","myJavaScriptFunction()");
The above code adds a JavaScript "onclick" to your button...so now when the user clicks the button your JavaScript method is executed.

I tend to put this code in the PreRender event...you only need to set this the first time the page is loaded (so long as your button and page have viewstate enabled).

So, in the PreRender event (or PageLoad event) you would have something like (C# code):
Expand|Select|Wrap|Line Numbers
  1. if(isPostBack==false)
  2. {
  3.     myButton.Attributes.Add("onclick","myJavaScriptFunction()");
  4. }
Please check out this article about how to use JavaScript in ASP.NET and the article named how to check if a TextBox contains a number for more information.
Feb 27 '09 #7
MuntaziM
7 New Member
well u need 2 get few references and use raisecallbackEvent(). see if u can get somethin from below lines :
Expand|Select|Wrap|Line Numbers
  1. Protected void Button_Click(sender object,EventArgs e)
  2.         {
  3.             // Get javascript function from the
  4.             // server that is capable of connecting
  5.             // to our server. RecieveServerData is
  6.             // the javascript method we will call when
  7.             // we are done processing the data
  8.             string reference =
  9.                this.ClientScript.GetCallbackEventReference(
  10.                this, "arg", "RecieveServerData", "");
  11.  
  12.             // Generate the javascript that can connect
  13.             // to the server
  14.             string callBackScript =
  15.                @"   function CallServer(arg, context)
  16.             {" + reference + ";}";
  17.  
  18.             // Put the javascript on the page
  19.             this.ClientScript.RegisterClientScriptBlock(
  20.                this.GetType(), "CallServer",
  21.                callBackScript, true);
  22.         }
  23.  
  24.         ///
  25.         /// This is the method called by the
  26.         /// javascript. Here you can do some server
  27.         /// processing to manage what you are going
  28.         /// to give back to the client if anything.
  29.         ///
  30.         ///
  31.         public void RaiseCallbackEvent(
  32.            string eventArgument)
  33.         {
  34.             /* Im saving stuff here */
  35.             this.fromClient = eventArgument;
  36.         }
  37.  
  38.         ///
  39.         /// This is the method that passes back
  40.         /// to the javascript.
  41.         ///
  42.         ///
  43.         public string GetCallbackResult()
  44.         {
  45.             return this.fromClient;
  46.         }
  47. }
Mar 1 '09 #8

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

Similar topics

14
5419
by: tshad | last post by:
I posted this on the asp.net group, also. I wasn't sure whether this was an asp.net problem or a javascript problem. I have a page that was originally created from a program I found on the net...
0
6413
by: Michelle Keys | last post by:
I am trying to call a print function to print a string from a database using javascript. Which is RC_DATA of Varchar2(2500). This is a javascript is not being used. I have a thing that needs to...
2
4290
by: jm | last post by:
I have a parent window: <script language="javascript"> function doSearch() { result=showModalDialog("searchmni.aspx?lastname=smith"); alert(result); } </script>
4
2979
by: Julia | last post by:
Hi Everyone, I am using webbrowser control to post data to an aspx page. However, for some reason, the aspx page sometimes will execute page_load event twice, and sometimes execute it once. So I...
5
4920
by: Scott Natwick | last post by:
Is there a way to execute a JavaScript from the Page_Load event? Or alternatively, is there a way to have it execute when the page is loaded? I am defining the script in the Page_Load event and...
4
4054
by: Ian Kelly | last post by:
Hi All, I have an .net form that is split into two frames. The left frame has a tree that displays a list of all the customers. The right frame displays the appropriate clients information. ...
0
2076
by: oliver | last post by:
QUESTION: How to access an object embedded in a UserControl through Javascript file? Another way to ask this question: How to execute script from a UserControl, accessing other objects in that...
1
10216
by: Veerle | last post by:
Hi, I have a web page and at the end of the page I have inserted some javascript that checks if Acrobat Reader plugin is installed. If it is not installed, I disable some elements (buttons,...
9
2206
by: Matthew Wells | last post by:
OK, I've narrowed down the problem. This works when in the aspx page <script type="text/javascript" > function btnFirst_Click() { alert("Hello");...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
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...
1
6894
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.