calling c# function in javascript function

I'm trying to call a c# function in my javascript function that I use as a buttons OnClientClick. The c# function simply changes some session variable values. The problem I'm having is that everytime my page posts back the c# function that is called within my javascript function is executing, and the only time it should execute is when the button is pressed. I'm not sure what I'm doing wrong but heres my code:


script type="text/javascript">




<%SetDefault(); %>





Please let me know if you have any ideas what Im doing wrong,


1/3/2007 3:06:46 PM 24353 articles. 2 followers. Follow

4 Replies

Similar Articles

[PageSpeed] 35

You cannot call server-side code from client-side code (unless you use something like AJAX). The only way for server-side code to execute is via a PostBack and page refresh.


1/3/2007 4:57:33 PM
Check the HTML that is output and see where your function is being called. It should only be on your button's OnClick event.
** Hope this helps!
1/3/2007 4:59:33 PM

<%SetDefault(); %>

I don't think you realzie this, but that line of code there is being executed every time your page is hit... ASP parses out any <% %> code blocks and runs it before you ever render the page to the client. Even though you think you put that code "in" your JavaScript function, it's not at all.

You should just do a regular asp:Button that posts back to the page and does the session stuff there... if you want to get fancy, then (as was mentioned above) you will want to use ASP.NET AJAX and make your asp:Button "trigger" asynchronously.

If you want to know how to do that, please say so.

-Timothy Khouri
Developer / Architect / Author
1/4/2007 4:14:55 PM

I think you can use Client Callback Feature of to accomplish this,

check this article -


When you ask a question, remember to click "mark as answered" when you get a reply which answers your question; this ensures the right forum member gets credit for being helpful (and makes search more relevant too).
1/8/2007 7:13:34 AM

Similar Artilces:

How to call server side(C#) function from Client side(Javascript) function?
Hi i have used below functions,  function HandleOnClose() {// this function will be called after clicking browsers close button in Internet explorer if (event.clientY < 0) { event.returnValue = 'Are you sure you want to leave the page?'; } // if he clicks ok then i need to call one server side function in this instance.. else not. } <body onbeforeunload="HandleOnClose()" leftmargin="0" rightmargin="0">other controls in page..</div>  How to achieve this? Thanks MS AJAX Extensions would be the option of choice here. Combined with webservices you can...

Can you call a c# on the server side from javascript functions on client side
Hello guys This is something intresting I have come across during development. I have a javascript written in a .JS file , to my knowledge the .js files are executed on the server side. But security concerns with java script that these .js files can be access by connecting to port 80 of the server . So an ultimate alternative reminas to call c# code from a java script function. Can anyone Help me on this matter Regards Nihar Hi Nihar, Usually .js files executed on client side. Please explain your configuration.Leon LangleybenMCSD, ASP.NET MVPBlog...

calling a javascript function from a c# function
Hi,I'm using, c# framwork 1.1 and some javascript.I have a table in which there are 3 cells.In each cell there is a control.In the first cell the control ID is DropDownList1 and the items are Item1, Item2 and Item3.In the second cell, the control ID is TextBox2.In the third cell, the control ID is TextBox3.What i am trying to do is :If DropDownList1 selected value equal Item1   then hide the second AND the third cells.If DropDownList1 selected value equal Item2   then show the second cell AND hide the third cell.If DropDownL...

Call c# function from javascript function
This is my code: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <%@ Import Namespace="System.Web.Services" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""> <html xmlns=""> <head runat="server"> <title>Untitled Page</title> <script runat= server> [WebMethod] public string GetName() { return "Hello Word"; } </script> <script language=javascript type="text/javascript">...

Need help converting a C#/.NET method into client-side (javascript) function
Hey Guys, I have implemented an AJAX Toolkit CalendarExtender.  I have a method 'CheckDate' in javascript (followed a tut!) that checks to see if the user selected a date > DateTime.Now(). Well, I have a few other things to check but not sure how to do them in javascript, so I'm hoping someone here can help me.  I mean, i can somewhat convert it but it'll be too mangled, I'd rather learn the right way.  I'm attaching the code and any help will be appreciated.  1 protected string CheckDateErrors(string dateGiven) 2 ...

How to call server side function from client side JavaScript?
I want to call a server-side function, like a button's click event or just another private or public function , from a client-side JavaScript function. How do I do that? thanX AnzIf this post was useful to you, please mark it as answer.ClientSideAsp.Net | Blog Maybe alittle more that you need, but....... 1) Put a button on the page. Lets say its id is Button1. Make sure that its has causesValidation=false 2) Write the code for the button. 3) Write the following in the PAge_Load event Button1.Attributes.Add("onClick", "myJSFunction(); return false") 4) Copy this javascript...

how to call server side function from client side javascript?
Hi all, I have a image button in the update panel. I would like to call a method from server side (not from webservice) to get the variable when Onclick on the image button is invoked. Is there any way that I can able to access this function from the client script? In using the UpdatePanel, in essence, you're already doing this because there will not be a full postback.  You can make an asynchronous call to your Click method of your ImageButton.  So, what do you want to do with this "variable"?Christopher Reed"The oxen are slow, but the earth is patient."...

Call server side function from client side javascript
I have a javascript where I either need to call an onclick even of a server link button or I need to be able to call a server side function.   The only thing is I cant use AJAX.  Is there any way I can do this or am I spinning my wheels. Thanks!-------------Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue. I have a similar problem.  In my case, I would like to set a server-side session object from JavaScript.  Any suggestions?  Thanks.  Dan Sure, y...

how to call server side function in javascript function
right now I have a question. I have a javascript function which created a textfield. what I want to do is when the user type in the value into the textfield, it will call the function in the server side and search the data in the database. I wonder is it possible to do that? And how to do it, can anyone some me some example..thx    look into ajax autocomplete Liming XuJumptree ASP.NET 2.0 Project Management - With Source Code, Free to Non Profit Org and Effectively Manage Projects/Tasks/Milestones Superwong-You definitely want to use Ajax (as was suggested). Since you're u...

call a server side function from javascript function
hello i have apage that is contained in MasterPage (contents) i have a HyperLink and i want to call a serverside function is it possible? i am writing C# nirnironIf i helped you please mark as answered.. in your page_load event, you can reference controls on the masterpage by MasterPage.FindControl("....")  Peter Kellner and blogging athttp://PeterKellner.netMVP, ASP.NET  Use the "LinkButton" control...  Looks the same as a hyperlink, acts the same, and you can catch events from it. no... ok this is the scenario... i have ...

How to call a javascript function from a server-side function
Hi. I'd like to know if it's possible to call a javascript function from within a server-side function. I know it's possible to associate a javascript function with a control event but I'd like to call it from within an function.  Thanks, Samir Zattar Hey, There are several ways to do it, all through the Page.ClientScript property.  You can write a script through RegisterClientScriptBlock or RegisterStartupScript, or if you have a script file you can write a reference to that file.Brian"Trust in the Lord and do what is good; dwell in the land and ...

calling server-side c# function in javascript
will you please help me on how to call a c# method in javascript. the known solution using adding an onclick event to a button and firing postback is not what i have to do. The problem is that i need to be able to call any function. Hi, you can also use the __doPostBack Javascript method on the client to call a serverside function. In the page_load event you can check which function is requested, e.g.:<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Evaluator.aspx.cs" Inherits="Evaluator" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www....

want to call a function of from javascript
hi everyone. i want to call a function of from javascript. and i also want to pass an argument from javascript to  that function. can anyone help me? thanks in advance. shweta  ThanksshwetaIn a day when don't come across any problem -you can be sure that you are traveling in wrong path Can you be more precise about the effect you're trying to achieve?  Explain you requirements , what you want to achieve be thisIf this post helps you, please mark it as Answer.Cheers,Muhammad Rizwan JavedSr .Net DeveloperAim 168, Dubai, UAE. Hi Try this, U'r Codeb...

How to javascript function from C# function ?
i have a popup function in the javascript. when ever i call it from HTML controls's onClick event it is working well. but when ever i writeResponse.Write("<script>"); Response.Write("PopupPicker(215,200);"); Response.Write("</script>"); in c# method it doesn't work any javascript function is as follow:<script language="javascript"> function PopupPicker(w,h) { var PopupWindow=null,settings; settings='width='+ w + ',height='+ h + ',location=no,directories=no,men...

Web resources about - calling c# function in javascript function -

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

Lightbox (JavaScript) - Wikipedia, the free encyclopedia
On a Lightbox-enabled page, a user can click an image to have it magnified in a Lightbox window, which resizes itself according to the size of ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
about dmoz - help Top : Computers : Programming : Languages : JavaScript : Tools : Obfuscators Tools for obfuscation/compression JavaScript code. ...

Facebook File-Transfer App Pipe Relaunches, Now Works Via JavaScript, WebRTC
... allowed Facebook users to send and receive files up to 1 gigabit in size. A new version of Pipe debuted Monday, and the app now works via JavaScript ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

JavaScript SDK - Facebook-Entwickler
Overview The JavaScript SDK provides a rich set of client-side functionality for accessing Facebook...

Senior Front-End Developer (Javascript and AJAX)
Company: Sometrics Category: Software Development Location: Los Angeles, CA How to Apply: Description: We are looking for ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken .… ...

Resources last updated: 12/7/2015 8:54:56 AM