How can I call a .net method in inline code using a javascript variable?
My code looks like this:
for (var i = 0; i < numberIterations; i++)
{
var result = <%# GetFilterTagURL( myArray[i].Value, false) %>;
//do stuff with result
}
This block is inside a javascript block; the GetFilterTagURL method is a a .net method and I want to pass the variable myArray[i].Value as the first parameter.
Any ideas?
Update:
All your answers are helpful.
I guess that, as you say, I'll have to create a web service to achieve what I want.
You will not be able to accomplish it like that, since the <%# %> tag is used for Data binding.
One approach however would be to create a webservice and call it from your javascript. Inside your webservice you can then make the call to GetFilterTagURLand use the result in your javascript.
Check out this article from MSDN for more info.
You cannot send client-script values to the server-side. The oposite is valid though, you can register a javascript block from your code-behind.
See Page.RegisterStartupScript method.
this will work, but you have to call Page.DataBind() in your code
Related
I have a JS function which I want to loop round a c# object passed from the controller. I then want to compare it to a HTML input value - which I have passed into the JS function as a string.
Function is called like
searchReleases($("[name=release]").val());
function searchReleases(givenName) {
var list;
var data = givenName;
#foreach (var a in #Model.Releases) {
if (#a.Name.Contains(givenName)) {
list+= a.Name + "=";
}
}
}
However I can't access the JS variable givenName within the IF statement.
I have spent a while trying to find an answer on google but have yet found a workable solution.
You should understand that C# is a server-side code and JS is executed on client-side.
So if you generate view in ASP.MVC you have access only to C# data. All information which you want to print on your page will be statically generated to HTML file.
Then you can pass this HTML page to browser and inside it, it would be executed JS code. But only based on statically generated data or JS variables.
You can't read JS from C#, but you can read C# from JS. So if it's a read-only operation, than convert your C# object to JSON and access it from the JS code.
Try this answer : Asp.net mvc passing a C# object to Javascript
I'm new to RichFaces. I have a requirement to call backingbean method from javascript. I used a4j:jsfuction to do so but I was adviced not to use this component for performance and call backing bean method directly within javascript like below
within javascript:
somemethod('somevalue')
within xhtml:
function somemethod(value){
#{backingbean.test(value)}
}
Can you please let me know which approach is better and why?
Thanks in advance.
a4j:jsFunction is better since the "pure JavaScript" solution will not work the way you want:
function somemethod(value){
#{backingbean.test(value)}
}
EL expressions like this will be executed when the page is being rendered, passing the parameter will not work either. Have you actually tried if it works?
I am using laravel 4 and I want to pass a data with a view.
I have used this code in a controller.
$view = View::make('settings.editEvent');
$view->bounderyData = $bounderyData;
And I want to check whether this data exists or not in the view settings/editEvent.blade.php
Tried using this..
<script>
if('{{$bounderyData.length()}}'!=null)
console.log('exists');
</script>
Error :
Array to string conversion error
How can I check the existence ?
Do not assign the data to the View variable, but instead, pass it along using with as Laravel requests you to use:
$view = View::make('settings.editEvent')
->with('bounderyData', $bouderyData);
Actually both of the snippets work the same way. You can either pass data using with() method or by assigning it to view as property. So it doesn't really matter. But it looks like you are using some weird syntax because you are trying to access method length() using dot syntax inside Blade echo statement. Try:
if({{count($bounderyData)}}!=null)
console.log('exists');
or something similar. Remember that everything inside {{}} is going to be echo'ed by PHP. So if you have some sort of array there you may either want to count number of elements or maybe cast it to JSON and then decode it inside Javascript. If you still have problem, let us know what is the issue.
I am using Rails 3.0.5
I have a javascript code that uses jQuery dropdown checklist and I would like to have the checkboxes displaying which item is checked already and which is not, when using my form in the 'Edit' action of my belongings controller.
Basically, it can work like this in Javascript:
$('element').val([1,3,4]);
$('element').dropdownchecklist();
Let us say that in my controller, edit action, I have a instance variable called #selected_items that contains the items that are selected (here: #selected_items = [1,3,4]) and I would like to use the value of this instance variables into my Javascript code, how could I proceed ? How could I access the value of this instance variable in a .js file ?
Many thanks for your answer !
Just write the javascript in your view, and at the appropriate place, print the #selected_items array as json:
app/views/*/*.html.erb
<script>
$('element').val(<%= #selected_items.to_json %>);
$('element').dropdownchecklist();
</script>
You can also use the Gon gem if to_json cannot serialize more complicated objects.
A better and a clean/neat way of handling such requirements is
1.Don't create javascript inside the views not as per the rails
best practices
2.Handle it this way
In your app/views/*/*.html.erb
where you are having the instance variable like (#selected_items) assign it into the options within the helpers something like :sel => #selected_items
In your javascript
<script>
$('element').attr('sel');
$('element').dropdownchecklist();
</script>
I hope it helps!
I have javascript function sample('textValue') and have to call at server side on anchor click. I tried below code
string text="xyz";
anchor.Attributes.Add("onclick","javascript:sample('"+text+"');
but the value of the text is not assigning correctly. Encoded string gets added. The result in view source looks like
javascript:sample('xyz')
But i need javascript:sample('xyz')
What server/backend language do you use? PHP? Do you use any framework (Zend, CakePHP...)?
On the JS side do something like this:
Option 1
Test
Option 2
Test
<script type="text/javascript">
document.getElementById('clicky-clack-link').onClick = function() {
sample('test');
};
</script>
Note: Also check out jQuery if you haven't.
I wonder if you could just do this:
string text="xyz";
anchor.Attributes.Add("onclick", function(){ sample(text); } );
What does it do? Well, the onclick handler takes a function with no arguments, right? That is, what to do if somebody clicks the link. If you're coding this by hand in HTML, you can use the javascript:a_statement_goes_here to describe the code to run. I expect the browser will just create a function out of that. Since you're assigning this in JavaScript, you have to do that yourself (unless you write out to the document - that might work) and assign the function. But you don't have such a function yet - you have one sample that takes an argument - hence the anonymous function closing the text argument.
This is based on the assumption, that the above is actually client-side code. I'd be very surprised, if JS didn't allow you to assign a function to an attribute. In fact, I think the problem you are running into, is JavaScript trying to be very smart and make sure assigning a string, will stay a string - that is why your ' got encoded.
Have a go, tell me how it went. Ta!