Executing JavaScript from the server code in asp.net - javascript

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.

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.

Is it possible to use a javascript variable inside of a javascript variable that contains php?

I'm trying to use a JS variable inside of php code that executes a function. Based on the selected radio button, Javascript gets a string of code from a data attribute of the radio button (trimcode) and I need to set this as the key in my PHP function where one of the variables requires an array key.
The result will be, whenever a radio button is selected, the PHP function inside this function will execute using the 'code' variable and thus return it's return value as the inside content of the "byo" class div via javascript innerhtml. However, I can't figure out how to use a javascript variable inside of PHP code that is inside another javascript variable. Can someone point me in the right direction?
<script type='text/javascript'>
$(document).ready(function(){
var code;
var colorsavailable;
$("input:radio[name=trimlevel]").click(function() {
code = $(this).data("trimcode");
colorsavailable = ("<?=byoTrimImageColors($modelMap["+code+"],$hexcolors)?>");
$('#byo').html(colorsavailable);
});
});
});
</script>
I think you're confusing the notions of server-side code and client-side code.
The PHP code runs once, in its entirety, when the page is requested. Then, after it's executed, the resulting page is sent to the browser where the JavaScript code runs. And that line of PHP code isn't going to execute for each click event. It's going to execute once, and only once, and emit only one result to the page.
Given what it looks like you're trying to do, you probably have two options:
Include the functionality you're invoking in JavaScript code and just invoke it there instead of in PHP code. Whatever that array is, whatever that function is, implement those in JavaScript. Then there's no need to involve PHP at all.
If the functionality needs to be in server-side code, then you're going to need to use AJAX to invoke it. Essentially you'd create a separate PHP "page" (which emits JSON data, not an actual page) to accept the value, invoke the functionality, and emit the result. The JavaScript code would make an AJAX request to that resource and use the result.
Client-side, that AJAX call might look something like this:
code = $(this).data("trimcode");
$.post('someNewPage.php', { code : code }, function (result) {
$('#byo').html(result);
});
Server-side (and I'm mostly guessing here), someNewPage.php might have something like:
$code = $_POST['code'];
// you'll want to sanitize the $code value here, since it's user input
$result = byoTrimImageColors($modelMap[$code],$hexcolors);
echo json_encode($result);
Your idea is not possible since for PHP code to execute it requires a page refresh. The best solution would be using Ajax to send the data to PHP asynchronously and changing your HTML, using JS, with the data that comes back from PHP.
To learn how Ajax/PHP works visit this site: http://www.w3schools.com/php/php_ajax_php.asp.
It is not possible to call a PHP function from Javascript. PHP is a pre-processor and ALL PHP code will finish executing before the page loads. Once the page loads, the browser will load your Javascript files and JS will take it from there.
In order to "call a PHP function" again after the page has loaded, you will need to make a new request to the page. This is typically handled in Javascript via AJAX. Using ajax, Javascript can send a new request to the server, PHP will read it and send a response back to Javascript.
If you really need that PHP function to execute on the Javascript level synchronously, then consider porting that function to Javascript.
Right now what you are doing is constructing a string that resembles some PHP code. It looks like you are doing that part just fine.
However, you cannot execute PHP code on the client side. There is no interpreter available on the client (the browser) that knows what to do with your PHP code.
What I would recommend doing is if you need to run some PHP code, create a controller method or some script that you can execute on the server, make an AJAX request to that (passing the JS code variable), have the PHP method return the value of your byoTrimImageColors method, and then handle that data however you please.

Call javascript function when consuming event in codebehind

I have a aspx page that, when loading, will initialize a class called, Bill :)
When Bill is initialized, the aspx page will wire a function to a handler of Bills.
When that function in Bill is fired, I would like to call a javascript function in the aspx page, but that is where I am having difficulties...
I have tried ClientScript.Register and all of that, but nothing is seeming to work. As of now, I am simple trying to get the function to fire an alert.
I have a feeling that I am missing something critical in the understanding of this, so any information would be greatly appreciated.
Thanks.
Is all of this happening in javascript, or is some of this server-side code? Javascript is client-side only, so if you are running server-side code, you can't execute the javascript on the server. You can, however, register script to run when the page renders in the browser (be careful of this, if you are opening another window, actions like this will be blocked by the browser unless the user directly initiates the action by clicking a button or something similar).

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.

JavaScript & JSP

Firstly, I have done my research and already know that JavaScript = client JSPs = server side. I don't want to waste your time.
My situation is that I want to execute JSP code from an event (not from a HTML form).
I have a HTML link (XXX), which is NOT within <form> tags; it just an ordinary HTML link. Through Javascript, I will be able to get the href value and store it in a hidden input field. Instantly after this, I want to execute request.getAttribute("...") and pass the parameter between JSP pages.
I do now know how to do the latter part (i.e. getting the request.getAttribute code to instantly proceed after the Javascript code has executed.
Can anyone advise?
You can't run JSP code like this.
JSP code, as you said is run on the server side, so you can't really trigger it from the web browser.
If you are just trying to pass a parameter between JSP pages, you can add the parameter to the queryString of the URL when you call the second JSP, and within it use request.getAttribute().
Remember, JSP code is executed before the page is sent to the browser.
I hope this helps.

Categories

Resources