I would like to do multi things with one button click. Is this possible if I'm using ASP.NET web page and javaScript.
I have one button, and onClick event I need to do somethings in backed, but I need as well to invoke one javaScript function on the front end.
Is this possible using C#? And if it is how can it be done.
P.S. I've found some solutions on internet, but all of them are obsolete.
Yes, ASP Buttons have the OnClientClick property which can be used to fire client-side JavaScript in addition to operations on the server side.
It's also possible to block server-side operations with based on the result of the client-side JavaScript - see this answer for more on how to do this.
Yes use an ASP Button control. Set the OnClick event to your server side function and the OnClientClick to your client side function.
Note if you want to execute both the client and server click events you need to either return true in the OnClick event to execute the PostBack, or remove the OnClick event all together and register the client side function as a start up script on the server side event.
Related
Is it possible to setup an asp button so that when it is clicked, the client-side javascript will run, and then the server-side c# method will run, but only after the prior javascript has completed? The goal being when the client submits a form, the 3rd-party jscript does its thing and sends out the form data. When that is done, the server-side method will run, create the file it needs to, and then post it back so that the client can download it. I have both of these working separately, but trying to put them together has been the issue.
Doing some google-fu and S.O. searching has led me to think that maybe the option would be having OnClientClick and OnServerClick attached to the same asp:button? Or is there a different way?
You just need to provide the javascript method for OnClientClick. It will be executed before the postback takes place. You can even prevent it by returning false:
function JsMethodName() {
// do something...
// return true; // postback
// return false; // no postback
}
<asp:Button ID="ButtonID"
OnClientClick="return JsMethodName();"
OnClick="ServerMethod" runat="server" Text="ButtonText" />
What is the event sequence of a a4j:commandLink or a4j:commandButton?
Is it right: onclick->actionListner->oncomplete->reRender?
Is reRender comes before oncomplete?
When action happens?
The onclick JavaScript is called the first when the enduser clicks the generated HTML element. When the JavaScript code does not return false, then all associated ActionListener implementations in the JSF side will be invoked, in the order of their association with the component. If the listeners haven't thrown any exception, then the real action will be invoked. When the action returns a successful response, then the components specified in reRender will be updated in the client side. Finally the oncomplete JavaScript will be called.
It's pretty easy to track yourself if you have a debugger in both the client and server side and know how to use it. I strongly recommend Firebug for the client side and Eclipse for the server side. You can of course also always do poor man's debugging using alert()s or System.out.println()s.
Here is what I need to do: I need to execute some server side code after a user clicks on a button, THEN I need to execute some client side code. OnClientClick gets invoked BEFORE the onClick; and what I need is the opposite.
Is there a way to do this? It looks like I will have to execute JS code from the server code.
You could use RegisterStartupScript in the code behind of your app.
This would place the javascript code you need to be called in the body.onload event.
I have a situation where I want to use several server side controls, which have clients side state. I want to check the state when events occur (like various clicks), and when the state is where i want it, I want to postback to the server and do some processing.
In particular, I need to make sure that at least 3 different controls have selections before I want to do a postback. Since there is no specific control that will initiate the postback, I just want to capture the selection events on the client side, then call __doPostBack() (or something similar) to initiate the processing. So i have disabled all server side events, turned off autopostback, and have wired up some javascript to handle this.
I've got all the client side code written and working, however I cannot seem to get the server-side to recognize the postback. I'm overriding RaisePostBackEvent, and checking the eventArgument for my custom argument. This doesn't work, as RaisePostBackEvent is never called.
This method has worked when I had autopostback enabled (for example, the Telerik Radgrid OnSelectChanged server side event).
Any suggestions on the best way to handle this?
Update:
When asked for sample code, it's exactly as I say above. Not rocket science.
Javascript:
function CheckState(source, eventArgs) {
// logic to test state of controls
__doPostBack("", "DoMyWork:");
}
Then in code behind I have:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
if (eventArgument.IndexOf("DoMyWork") != -1)
{
// do my server side work.
}
}
RESOLUTION:
Because this is somewhat confusing.. I'll just say what my solution was.
I used one of the controls involved in the postback and used it's UniqueID for the control parameter, then my method as the event.
if you call the __doPostBack() without parameters, asp.net cannot figure out which control fired the event and thus it cannot determine which event to fire.
you need to add the name of the control as a parameter to __doPostBack() and an argument which could be null if you dont need one
__doPostBack('textbox1','') //no arguments
__doPostBack('textbox1','12')
you can read the argument you passed in from code behind like this:
Dim arg As String = Request("__EVENTARGUMENT")
I need to call a .cs method from JavaScript function; how can I do that?
Thanks
You need to do post back to the server then catch this post back on Pageload event then execute the cs function.
check the following article:
Understanding the JavaScript __doPostBack Function
You need to postback to the server by either submitting an HTML form or using JavaScript. If you're not in a browser, it might be more of a challenge because I don't think the Windows Scripting Host includes anything that can call the web by default. In which case you could use something like cURL instead.