modify passed in variable to javascript - c#

i was just playing around with javascript/jquery within mvc 3 and was just curious as to whether there was a way to pass in a C# variable to javascript and then modify from within the script. Though playing around with it, i noticed that it was not possible, as the variable passed in was just the value, and not the address.
Essentially, what i wanted to do was change a bool value from false to true when a html button is clicked. I figured i could do it through javascript but ran into the aforementioned problem. Is there anyway of doing this? Better yet, i'm sure there is a way, but is my design pattern flawed?

This is how ASP.Net/MVC is setup... No you can't modify server side C# variables in JavaScript. JavaScript is run in browser while C# page's code run on server. All C# classes are created and destroyed for each request and JavaScript code does runs at the different time (again destroyed after each page navigation via GET/POST).
There are multiple ways to pass data to/from JavaScript and easiest is render on GET and do submit (POST) to return changed fields to server.

Related

avoid autopost back asp.net

i am working on a web application using asp.net c#. it has multiple textbox controls like
TextboxA TextboxB TextBoxResult1
TextBoxX TextBoxY TextBoxResult2
i am doing some calculation on TextboxA,B,X,Y and displaying result to TextBoxResult1 and TextBoxResult2 using Ontextbox_change event.
my question is that On each calculation my page is going to refresh.i need to know how can i do this without auto post back . i don't want to use code other than c# like Ajax, java script, jQuery etc.
AutoPostback=true;
thanks in advance
You can't. The code on a view is done at render and is then static html. In order to update the information you either do what you're doing (POST back) or use ajax to post back to the controller but only update specific information instead of refreshing the entire page. You could look into Blazor, but it's still experimental and not fully released (I can't comment on it, haven't used it myself yet).

JQuery UI Modal Form Dialog Save Data To Database

I am using the JQuery UI Modal Form Dialog and trying to save the old data and new data to a database. I am using C# (backend) and ASP.NET front end. I can delete new entries, I just don't know how to save the data. I have tried searching ways to pull pull the HTML data in, but couldn't get rid of the errors. Also Wasn't sure if there was a better method? Here is my JSFiddle
function addUser() {
I need to figure out a way to get the data from the table and post it to the server.
I think what you may be looking for is web methods, it allows the jquery (client side) to call the backend (c#) and for them to interact with each other.
Check out this link for reference:
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
Here is a brief summary from the website:
When it comes to lightweight client-side communication, I’ve noticed
that many of you prefer ASP.NET AJAX’s page methods to full ASMX web
services. In fact, page methods came up in the very first comment on
my article about using jQuery to consume ASMX web services. Given
their popularity, I’d like to give them their due attention. As a
result of Justin‘s question in those comments, I discovered that you
can call page methods via jQuery. In fact, it turns out that you can
even do it without involving the ScriptManager at all. In this post, I
will clarify exactly what is and isn’t necessary in order to use page
methods. Then, I’ll show you how to use jQuery to call a page method
without using the ScriptManager.
I found this:
This script gets data from the table that you can then parse through.
You could then pass it into a hidden field.

Accessing C# function in Javascript

I think there is something that I don't understand as I proceed forward in my project of importing D3 visuals into Spotfire.
I currently have implemented a D3 visualization into Spotfire by using Awesomium's embedded browser and these lines:
string myString = System.IO.File.ReadAllText(#"C:\Users\leear\Desktop\SDK\SDK\Examples\Extensions\D3Visualizations\Gauge.html");
webView.LoadHTML(myString);
The HTML of Gauge.html contains javascript code that runs all the necessary things to create the visualization.
However, I want to be able to access/run C# functions and variables inside the Gauge.html file. I am envisioning something like this:
for (var value in <%=Spotfire.getCurrentColumn()%>) {gauge.redraw(value)}
My project is currently structured just for the C# code to read and write all the HTML from a file into Awesomium's embedded browser.
How would I do this? Where does aspx.NET fit into this, if at all?
Thanks
You can't; javascript is client-side, and ASP.NET is server side. ASP.NET basically generates HTML and javascript, and once that rendering is done the page is processed the client-side takes over, which it knows nothing of ASP.NET. It's perfectly fine for ASP.NET to spit out JS becuase it's RENDERING it, but the client-side PROCESSING of JS, which is where you are trying to call a server-side method knows nothing of ASP.NET. You'd have to move the logic to the client, or have the client talk to the server using javascript.
I can't advise further as I don't know what that method does.
"You can't; javascript is client-side, and ASP.NET is server side. ASP.NET basically generates HTML and javascript"....
This is absolutely correct. Client and server are two different worlds...
You have a couple of options if you need to get code-reuse.
Look into a C# to javascript compiler..and make sure your C# functions don't depend on anything related to the server or its runtime.
Otherwise, consider exposing your server functionality as a web service that takes input of some form and returns output as JSON for use on the client side.

Referencing jquery variables in C# code (ASP.NET MVC)

How do I reference a jquery variable in a C# block in the view using ASP.NET MVC?
For example:
$(":input[#name='mydropdown']").change(function () {
var selection = $("#myselection").val();
pop($("#md"), <%= Model.choices[selection] %>);
});
Where the selection that is in my C# block is the same as the selection that is referred to in my jquery.
This is not possible to do. The C# code is executed before the HTML is sent to the user's browser, which is before jQuery gets loaded, which is before the variable selection has a chance to exist.
There are two approaches to work around this:
Dump all data that you care from Model.choices to a JavaScript variable; your JS code can then access that variable. This is simple and good if your data is not too large in volume.
Have the JS code make an AJAX request to the server to get whatever data it needs by passing the value of selection as a query string parameter.
Perhaps try Sharpkit plugin from jquery website:
http://plugins.jquery.com/project/SharpKit
You can't do this because of the browser (client) does not share any memory or state with the server what so ever
i.e.
the server executes the c# that renders the html and js
the browser downloads this and interprets it
the browser executes the javascript (no c#!)
I'd go with Jon's suggestion 1) as it will be more performant by negating the need for another callback to the server.
Long live ASP.NET MVC! :)

Update a field from server without reloading the existing page using simple javascript

I want to change a value of a field say document.getElementById('reloader').innerHTML = updated value from Server
I do not want to use Ajax, PHP, ASP, JSP .. or anything like these.
Is it possible by using simple javascript?
Server is C# 's application made by using HttpListener.
Please question if needed more info.
You can't do it without using something like AJAX, unless you're willing to update the entire page. Somehow, the browser has to contact the server, trigger an action there, and receive and process the response.
Thus, you can use XMLHttpRequest, or you could use JSONP or something similar. In any case something has to be written on the server to respond to the request and supply the data, and that's not going to be "simple Javascript" unless you've got a server-side Javascript solution (which is not impossible of course, but probably unlikely).
I am not sure why you dont want to use Ajax. But i believe the only other workaround then can be use an iframe on your page point to the server script and write javascript to read it. I havent tried it recently but i believe it should work

Categories