I'm writing a web page in ASP.NET. I have some Javascript, and I have a submit button with an onClick event.
Is it possible to call a method I created in ASP with Javascript's onClick event?
I'm writing a web page in ASP.NET. I have some Javascript, and I have a submit button with an onClick event. Is it possible to call a method I created in ASP with Javascript's onClick event? |
||||
|
Well, if you don't want to do it using AJAX or any other way and just want a normal ASP.NET postback to happen, here is how you do it (without using any other libraries): It is a little tricky though... :) i. In your code file (assuming you are using C# and .NET 2.0 or later) add the following Interface to your Page Class to make it look like
ii. This should add (using Tab-Tab) this function to your code file:
iii. In your onclick event in Javascript write the following code:
This will call the 'RaisePostBackEvent' method in your code file with the 'eventArgument' as the 'argumentString' you passed from the Javascript. Now, you can call any other event you like. P.S: That is 'underscore-underscore-doPostBack' ... And, there should be no space in that sequence... Somehow the WMD does not allow me to write to underscores followed by a character! |
|||||
|
The Another solution (very hackish) is to simply add an invisible ASP button in your markup and click it with a javascript method.
From your javascript, retrieve the reference to the button using its ClientID and then call the .click() method on it.
|
|||||||||||||
|
The Microsoft AJAX library will accomplish this. You could also create your own solution that involves using AJAX to call your own aspx (as basically) script files to run .NET functions. I suggest the Microsoft AJAX library. Once installed and referenced, you just add a line in your page load or init:
Then you can do things like:
Then, you can call it on your page as:
The last parameter of your function call must be the javascript callback function that will be executed when the AJAX request is returned. |
||||
|
This is the library called AjaxPro which was written a MVP named Michael Schwarz. This was library was not written by Microsoft. I have used AjaxPro extensively and it is a very nice library, that I would recommend for simple callbacks to the server. It does function will with MS version of Ajax with no issues. However I would note with how easy MS has made Ajax I would only use it if really necessary. It takes allot of JavaScript to do some really complicated functionality that you get from MS by just dropping it into an update panel. |
|||||||||
|
You might want to create a web service for your common methods. |
|||
|
If the __doPostBack function is not generated on the page you need to insert a control to force it like this:
|
||||
|
It is So easy for boath senarios (ie:synchrone/Asynchrone) if you want to trigger a server side event handler. for example Button's Click Event. for triggering an event handler of a control : if you added a ScriptManage on your page already then skip the step 1 1-Add the following in your page client script section
2-write you server side event handler for your control
3-add a client function to call the server side event handler
replace the "btnSayHello" in code above with your control's client id . by doing so if your control is inside an update panel the page will not refresh. that is so easy. one other thing to say is that be careful with client id because it depends on you ID generation policy defined with ClientIDMode Property. Enjoy! |
|||
|
Static, strongly-typed programming has always felt very natural to me, so at first I resisted learning Javascript (not to mention HTML/CSS) when I had to build web-based front-ends for my applications. I would do anything to work around this like redirecting to a page just to perform and action on the OnLoad event, as long as I could code pure C#. You will find however that if are going to be working with websites, you must have an open mind and start thinking more web-oriented (ie. don't try to do client-side things on the server and vice-versa). I love ASP.NET webforms and still use it (as well as MVC), but I will say that by trying to make things simpler and hiding the separation of client and server it can confuse newcomers and actually end up making things more difficult at times. My advice is to learn some basic Javascript (how to register events, retreive DOM objects, manipulate CSS, etc) and you will find web programming much more enjoyable (not to mention easier). A lot of people mentioned different AJAX libraries, but I didn't see any actual AJAX examples, so here it goes. (If you are not familiar with AJAX, all it is, is making an asynchronous HTTP request to refresh content (or perhaps perform a server-side action in your scenario) without reloading the entire page or doing a full postback. Client-Side:
That's it. Although the name can be misleading the result can be in plain text or JSON as well, you are not limited to XML. JQuery provides an even simplier interface for making AJAX calls (among simplifying other Javascript tasks). The request can be an HTTP-POST or HTTP-GET and does not have to be to a webpage, but you can post to any service that listens for HTTP requests such as a RESTFUL API. MVC4 Web API makes setting up the server side web service to handle the request a breeze as well. But many people do not know that you can also add Api Controllers to web forms project and use them to handle AJAX calls like this. Server-Side:
Global.asax Then just register the HTTP route in your Global.asax file so ASP.NET will know how to direct the request.
With AJAX and Controllers, you can post back to the server at any time asynchronously to perform any server side operation. This one-two punch provides both the flexibility of JavaScript and the power the C# / ASP.NET, giving the people visiting your site a better overall experience. Without sacrificing anything, you get the best of both worlds. References |
||||
|
Add this line to page load if you are getting object expected error.
|
||||
|
this reply works like a breeze for me thanks cross browser
Another solution (very hackish) is to simply add an invisible ASP button in your markup and click it with a javascript method. From your javascript, retrieve the reference to the button using its ClientID and then call the .Click() method on it.
|
||||
|
You can use |
||||
|
try this
Or this
use OnClientClick property of the button to call javascript functions... |
|||
|