Running Server Method after Client Javascript Method - javascript

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" />

Related

Invoke javaScript function on click event of ASP.NET button

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.

Executing JavaScript from the server code in asp.net

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.

Postback without a control context

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")

How to call a .cs method from a JavaScript function?

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.

What does "javascript:_doPostBack('Gridview','Edit$0')" mean, full post back still occurs?

JavaScript is used where one want to do something on the client side purely, or wants to send something to the server in a manner that postback does not handle.
But in Visual Studio 2008 controls ASP.NET C# I have seen that when the page is displayed in the browser the controls, namely GridView, FormView, and LINKBUTTON (!) all show this javascript:thing when the cursor is hovered on them. Why?
Post back still occurs. Even the linkbutton has this JavaScript thing and whenever you click on it, full post back occurs.
Changing label.text, etc. too is on the pageload event!
so why the JavaScript? Why not a simple button? Why linkbutton?
In this case JavaScript calls could be used to send additional data to the server, e.g. save some client data for the grid (like the width of resized columns or something like that).
Server-side frameworks use this approach to allow server-side guys to generate all the client-side code. It's a kind of quick'n'dirty solutions (comparing with well-organized unobtrusive JavaScript).
ASP.NET is stateless. That is, every time a page is requested, the server actually constructs the entire page and its controls & state and then responds to the request. It then renders the appropriate HTML markup as response to the request.
For any control, if there is the autopostback property set to true then a page is postback to the server if the control causes a postback (like clicking on a link button).
How does ASP.NET post back the page ?
It does it using a javascript function called _doPostBack(). The function is -
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
This function is used to submit the form back to the server. _doPostBack accepts arguments - event target and event arguments by using hidden variables __EVENTTARGET and __EVENTARGUMENT. This tells the server which control caused the postback and also passes appropriate arguments to the server.
if you have this code in your aspx page -
<asp:LinkButton ID="lnkButton" runat="server">LinkButton</asp:LinkButton>
The corresponding generated markup will be -
<a id="LinkButton1" href="javascript:__doPostBack('lnkButton','')">LinkButton</a>
So, say you click on a link button, the page is postback by the __doPostBack() function. Then, the page is recreated at server with the respective control state on the page. To get the state of each control on the page mechanisms like viewstate are used. Once the page is loaded, the server computes and renders the response markup.

Categories

Resources