How do you pass variables from c# to javascript?
Nevermind I think I figured it out. Assuming the code above, you can write in javascript:
<script type="text/javascript"> var JavascriptBlah = '<%=blah%>'</script>
This will pass the blah in c# to the JavascriptBlah on the client side. Then you can manipulate on client side.
How to pass variables from Razor to JavaScript?
You can do
<script type="text/javascript">
var p1 = @p1, p2= @p2;
</script>
and use p1, p2.
var data = {
labels: [2012, 2013, 2014, 2015, 2016, 2017],
datasets: [
{
label: "My Chart Label",
fill: false,
lineTension: 0.1,
data: [p1, p2, 50, 48, 47, 52]
}
]
};
In this case you don't have to use hidden html fields and you can expand your code to use more field like
var tempObj ={
tempData: "",
otherData:"",
moreData: ""
}
passing variable values from c# to javascript
When I need to pass variables into JavaScript I usually I prefer var clientVariable = '<%=ServerVariable%>';
solution. This method is sufficient for small number of scalar variables. If you need to pass a complex object or a array, consider to use JavaScriptSerizlizer
.
The behavior you are having it might happen for number of reasons. One of them might occur, if you have included a scriptlet into .js
file, and not into .aspx
file.
Here is how I would do this:
webgl-draw-file.js:
window.WebGlDraw = function(points /* point array */)
{
// Draw points here
}
Page1.aspx.cs:
public string GetSerializedServerVariable()
{
new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ServerVariable);
}
Page1.aspx:
<html>
<header>
<script src="webgl-draw-file.js"/>
<script type=text/javascript>
window.WebGlDraw(<%=this.GetSerializedServerVariable()%>);
</script>
</header>
<body>
...
</body>
</html>
To understand a better what values are passed to the JS function, take a look at a page source using your browser. You should see a JSON representation of your array instead of <%=Page.GetSerializedServerVariable()%>
scriptlet.
It should look something like this:
<html>
<header>
<script src="webgl-draw-file.js"/>
<script type=text/javascript>
window.WebGlDraw([{x:1, y:2}, {x:1, y:2}, {x:1, y:2}, {x:1, y:2}]);
</script>
</header>
<body>
...
</body>
</html>
.NET Core Pass Variable to Javascript from C#
You should use [FromBody] to get the json format data. Change it like below:
$.ajax({
//...
contentType: "application/json; charset=utf-8",
data: JSON.stringify(content),
//...
})
Controller:
public ActionResult SayfaAra([FromBody]string itemName)
How to Pass Variables to Javascript from C# using Invoke?
It takes some time until the WebBrowser
control has loaded the page so that it knows the Javascript function. Instead of calling the function directly after the load, use a handler for the LoadCompleted
event to run the function, e.g.:
public MyForm() // This is the constructor of your form/page
{
InitializeComponent();
wb.LoadCompleted += WebBrowser_LoadCompleted;
}
private void WebBrowser_Clicked(object sender, RoutedEventArgs e)
{
wb.Navigate("C:/Users/intern3/source/repos/MarketingProject/Samples/WPF/RESTToolkitTestApp/index.htm");
}
private void WebBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
wb.InvokeScript("SetCoords", new object[] { coord1, coord2 });
}
Above code adds the handler for LoadCompleted
manually, but you can also add it in the designer.
Pass a value to javascript from code behind
try this, this should work
ScriptManager.RegisterStartupScript(this, typeof(string), "script1", "SampleJSFunction('" + vls_variable.Text+ "');", true);
Related Topics
How to Change The Colour of The Line Below/Border of a Textbox (Entry)
How to Get Max Value of a Column Using Entity Framework
Windows Form Application Exception
How to Get Device Token in iOS 13 with Xamarin
Open iOS Application from Unity
Linq-To-Sql: Recursively Get Children
.Net Class to Execute Remotely on Linux Over Ssh
How to Password Encrypt SQLite Database
Print HTML Document from Windows Service in C# Without Print Dialog
Dotnet.Highcharts: Cost Not Plotted Against the Correct Date
Linux to Windows Bad Encoding Response
Soap Authentication Fails When Running a C# App on a Linux Box
Encryption Compatible Between Android and C#
Mvc-Web API: 405 Method Not Allowed
Escaping the Escape Character Does Not Work - SQL Like Operator
Are P/Invoke [In, Out] Attributes Optional for Marshaling Arrays