i have small class like
public static class TSM
{
static string TokenID = "";
public static string GetTSM()
{
TokenID = new Guid().ToString();
return TokenID;
}
}
`GetTokenID `will return a string
i just call GetTokenID from my aspx page like
<script language="javascript" type="text/javascript">
var token= <% =TSM.GetTSM() %>;
i am getting error when i am running that aspx page.
the error is
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1002: ; expected
please guide me what is going wrong. thanks
You need to bring the namespace in which this TSM class is declared into scope in your Web Page:
<%# Import Namespace="SomeNamespace" %>
...
<script type="text/javascript">
var token = '<%=TSM.GetTSM() %>';
</script>
or fully quote it:
<script type="text/javascript">
var token = '<%= SomeNamespace.TSM.GetTSM() %>';
</script>
or include it in the <namespaces> section of your web.config.
Remark: Notice the '' around the <%=TSM.GetTSM() %> as there is no Guid type in javascript. You must use a string.
Also note that your server side code is not thread-safe because of the assignment of this static field. Also it will always generate an empty guid.
Here's how to improve it:
public static class TSM
{
public static string GetTSM()
{
return Guid.NewGuid().ToString();
}
}
Related
Can somebody help in displaying webapi data in angularjs repeat directive in my first Angular application?
I'm getting data from the WEBAPI as expected like below
[{"FLAVOR_ID":"BES","FLAVOR_NAME":"BES"},{"FLAVOR_ID":"BUN","FLAVOR_NAME":"BUN"}]
API Controller:
public class ItemMaintenanceController : ApiController
{
ItemMaintenanceRepository itemRepository;
public ItemMaintenanceController(ItemMaintenanceRepository _itemRepository)
{
itemRepository = _itemRepository;
}
[HttpGet]
public IEnumerable<MA_Flavor> GetAllFlavors()
{
return itemRepository.GetAllFlavors();
}
}
Client.html:
<!DOCTYPE html>
<html>
<head>
<title> Client</title>
<script src="Scripts/angular.js"></script>
<script src="Scripts/angular-resource.js"></script>
<script>
alert("start");
var app = angular.module("myapp", ['ngResource']);
var controller = function ($scope, $resource) { // controller uses $resource, which is part of ngResource
$scope.flavor = {};
$scope.getFlavors = function () {
alert("calling getflvors");
var request = $resource("http://localhost:55762/api/ItemMaintenance/GetAllFlavors?Id=CMN");
$scope.flavor = request.query();
};
////$scope.clear = function () {
//// $scope.flavor = {};
//// $scope.error = "";
////}
$scope.getFlavors();
myapp.controller("ItemMaintenanceController", controller);
</script>
</head>
<body ng-app="contacts">
<div ng-controller="ItemMaintenanceController">
<select ng-model="flavor">
<option ng-repeat="fl in Flavor" value="{{fl.FLAVOR_NAME}}">{{fl.FLAVOR_NAME}}</option>
</select>
<h1>You selected: {{flavor.FLAVOR_NAME}}</h1>
</div>
</body>
</html>
Looks like issue with the names of the ng-controller, ng-module and ng-app. Try to change and hope it will work.
<body ng-app="contacts">
<div ng-controller="ItemMaintenanceController">
<Script>
var app = angular.module('contacts', []);
// To fetch all falvors
app.controller("ItemMaintenanceController", function ($scope, $http) {
.....
...
</script>
I think you're close. based on what you have I think something like this should work:
<select ng-model="selectedFlavorId">
<option ng-repeat="fl in flavor" value="{{fl.FLAVOR_ID}}">{{fl.FLAVOR_NAME}}</option>
</select>
your data is in $scope.flavor and I assume that if you debug it will look like this:
$scope.flavor = [{"FLAVOR_ID":"BES","FLAVOR_NAME":"BES"},{"FLAVOR_ID":"BUN","FLAVOR_NAME":"BUN"}]
you want the id in your value field as that is the bit you need to know which value you selected. the value you see in dropdown should be the name of the flavor.
when you select something, that value will be reflected in the model.
as I chose selectedFlavorId, you will find that populated under $scope.selectedFlavorId. Do not override your API data with the selected value like you've just done.
selectedFlavorId will give you the ID of the item you selected so you need a bit more code after this to get the name of that property from your data array.
There may following issues in your code.
Your angular modules defined as myapp and in ng-app you have used
contacts.
You have to create two different scope variables, one for the flavor
and another is flavors. flavors you need to use under the ng-options
and flavor you have to use for ng-model.
What I understood request.query() will return the resource object,
So have two options to get data from the query. More details about
the resource you can find here
var request = $resource("http://localhost:55762/api/ItemMaintenance/GetAllFlavors?Id=CMN");
Option 1
request.query(function(data) {
$scope.flavor = data;
});
Option 2
request.query().$promise.then(function(data) {
// success
$scope.flavor = data;
}, function(errResponse) {
// fail
});
I have the following extension method for jQuery to serialize Json called jquery.extension.js:
jQuery.fn.extend({
serializeJSON: function () {
return this.serializeArray().reduce(function (result, item) {
result[item.name] = item.value;
return result;
}, {});
}
});
And I have these references on my layout:
<script src="~/Content/Script/jquery.extensions.js" type="text/javascript"></script>
<script src="~/Scripts/jquery-1.8.0.js" type="text/javascript"></script>
On my view I call the method like this:
function createList(e) {
var form = $(this),
dataJSON = serializeJSON(form),
$saveBtn = $('#create-list-btn');
But I'm getting the error:
ReferenceError: serializeJSON is not defined
Could anyone help me realize what is missing here?
I found the problem myself, the proper way to use it on the view is:
dataJSON = $(form).serializeJSON()
instead of:
dataJSON = serializeJSON(form)
I try to add the following javascript code.
<script>
#if (ViewBag.checkedArtikel != null)
{
foreach (int ac in ViewBag.checkedArtikel)
{
String temp = "'#addartikel" + ac + "'";
<text> $(#temp).toggleClass('down');</text>
}
}
</script>
If i leave out the script tag i get the right jquery commands:
$('#addartikel1').toggleClass('down');
But with the script tag i get this error:
Uncaught SyntaxError: Unexpected token &
You have very badly mixed server side Razor code with client side javascript. Here's the correct way to do that, by using a JSON serializer:
<script type="text/javascript">
var articles = #Html.Raw(Json.Encode(ViewBag.checkedArtikel ?? new int[0]));
$(articles).each(function() {
$('#addartikel' + this).toggleClass('down');
});
</script>
Use this code:
<text>$(#Html.Raw(temp)).toggleClass('down');</text>
Or you can use your old code without adding quotes to variable:
String temp = "#addartikel" + ac;
<text> $('#temp').toggleClass('down');</text>
I want to retrieve property value in jquery function and want to set it in variable. I am trying with below code but its not working.
.cs file
private string _Heading;
public string Heading { get { return _Heading; } set { _Heading = value; } }
.aspx
<script type="text/javascript">
$(document).ready(function () {
$("#btnShowSimple").click(function (e) {
ShowDialog(false);
e.preventDefault();
var someProp = "<%= this._Heading; %>";
alert(someProp);
});
</script>
What should i do to retrieve property value?? Anyone have solution than please help me in this.
Firstly, if the variable is private you will not be able to access it in your JavaScript so you should use the public Heading property instead.
Secondly, you have a semi colon in your JavaScript which needs to be removed so change this line
var someProp = "<%= this._Heading; %>";
To this
var someProp = "<%=this.Heading%>";
I have the following block of code in my header:
<script type="text/javascript">
$(document).ready(function () {
$('#target').readmytweet({
'color': 'black',
'search': 'from:' + <%= GetUserName() %>,
'user': <%= GetUserName() %>,
'width': 600,
'tweets': 10,
'speed': 25
});
})
</script>
protected string GetUsername()
{
return "somestring..";
}
However, I am getting an error message:
The Controls collection cannot be modified because the control
contains code blocks (i.e. <% ... %>).
Does anyone know how I can pass a C# variable from my code behind into this jQuery function without getting that error?
Thanks in advance
For a dynamic string:
That seems like it would work, try wrapping the code blocks with quotes, as such:
'<%= GetUserName() %>'
also you may have to use a this statement to access that method:
'<%= this.GetUserName() %>'
For a static string:
Declare your string as a public string in your aspx page:
public string UserName = "somestring..";
and access it via:
var userName = <%=this.UserName%>;
This is a well-known problem when trying to add controls to a page that contains code blocks.
A simple workaround is to use data binding expressions instead, i.e., to use <%# ... %> instead of <%= ... %>. Note that you will have to call this.DataBind(); in your Page_Load event for this to work.
(BTW, remember that the code you insert in JavaScript will need to be properly quoted.)
Accessing a server-side c# variable/property within an .aspx page.
<script type="text/javascript">
<% string username = Class.PropertName; %>
var jsUsername = '<%: username %>';
</script>