By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,153 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

How to Execute a JavaScript Function from .aspx page

P: 83
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 can i pass these values to the javascript function which is thr in master page 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
  7. value1="";
  8. value2="";
  9. value3="";
  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..
Feb 26 '09 #1
Share this Question
Share on Google+
7 Replies

Expert Mod 5K+
P: 8,639
to me it looks more like Java....
Feb 26 '09 #2

P: 83
hey NO ,im using ASP.NET and C#.
Feb 26 '09 #3

Expert Mod 5K+
P: 8,639
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

P: 83
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..
Feb 26 '09 #5

Expert Mod 15k+
P: 16,027
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

Expert Mod 5K+
P: 9,731
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 now when the user clicks the button your JavaScript method is executed.

I tend to put this code in the PreRender 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

P: 7
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", "");
  12.             // Generate the javascript that can connect
  13.             // to the server
  14.             string callBackScript =
  15.                @"   function CallServer(arg, context)
  16.             {" + reference + ";}";
  18.             // Put the javascript on the page
  19.             this.ClientScript.RegisterClientScriptBlock(
  20.                this.GetType(), "CallServer",
  21.                callBackScript, true);
  22.         }
  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.         }
  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

Post your reply

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