473,566 Members | 2,908 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,passva lues(), 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.R egisterStartupS cript will work for me ...if yes kindly tell me how can i use tht .

Thanks in advance..
Rgds,
BTR.
Feb 26 '09 #1
7 10640
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 RegisterStartup Script 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 raisecallbackEv ent(). 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
5437
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 that works well as an html page. It brings up a modal popup window that I have been trying to work out for days now and this was the closest I...
0
6421
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 be modified: if(e.CommandName =="Print") { string parsedreceipt = null; parsedreceipt = DecodeReceipt (e.Item.Cells.Text); Session =...
2
4297
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
2985
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 might get different results for executing it. If I have the webbrwoser control to call it the first time, the aspx will usually execute the...
5
4924
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 using the RegisterStartupFunction to add it to the page. I just need a way to execute the script when the page loads. Any ideas?
4
4056
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. When the save button is pressed on the right frame, I want to update the tree in the left frame AFTER saving all the data as the changes to the right...
0
2085
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 UserControl? (Script attached to, and executed by, a UserControl embedded server control can ‘see’ the UserControl through the root document...
1
10232
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, checkboxes, ...) on the page. This javascript is at the end of the page, because javascript cannot identify html elements if they are defined below the...
9
2212
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"); alert(document.getElementById("<%=LBFacilities.ClientID%>").options.length); return false; } </script>
0
7673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8109
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7645
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7953
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5213
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.