I'd like to know whether it's possible to pass data from a C# application (e.g. WPF) to a web-page (i.e. an HTML page with Javascript) in an embedded Web Browser. If so, how?
Thanks in advance for your help!
You can invoke methods (and thus pass data) in your C# code from JavaScript using Window.external. Try googling for this keyword, more information will come up quickly.
This blog post has a good, simple (WPF-)example how to invoke a C# method from JavaScript using Window.external. Another keyword here is [System.Runtime.InteropServices.ComVisibleAttribute(True)]. You have to mark your handler class with this attribute to be accessible from JavaScript. And finally you tell your WebBrowser control about the handler class via its ObjectForScripting property.
Related
I am developing a project using Xamarin.forms. I need to get data from webservice and then need to show that data on WebView HTML page. Suggest me any Xamarin.forms inbuild function or any renderer class to implement this feature.
To send data from C# page to HTML we need to call javascript function defined in that HTML file.
We are having Eval function in Xamarin Forms.
WebViewObj.Eval(string.Format("JSFunctionName({0})", DataInStringFormat));
By using above function we can send the data to JavaScript function.
You probably want to look at the documentation for creating a HybridWebView.
This document describes how you can create your own kind of WebView which can handle JavaScript events for you. Although it may not be 100% accurate to what you want, it will give you an idea and you can pick it up from there.
As you noted in the comment you need to implement C# to JS. In the same custom renderers you can call functions for this on the WebViews.
For iOS you can use InvokeJavaScript(); and for Android (mis)use LoadUrl("javascript: alert('Hi!');").
For more info on this, follow this great post by Adam Pedley.
I immediately apologize for any misnomers that I may use.
I have a series of web pages that are generated by PHP that I am manipulating using the WebBrowser control. These pages have defined JavaScript functions buried inside the body (which is stupid to me, I didn't write them, I just have to work with them).
Is it possible to access these functions by using the Document.InvokeScript method, and if not, what can I do to access them?
Windows forms's implementation uses IDispatch.GetIDsOfNames to look for an exact match of the function name in the top level named items of the script engine. For MSHTML's JScript implementation of active script interfaces, the top level named items are
members of jscript globals (note this is assuming you code is written in jscript, for globals that are visible to vb script code, check VBScript Language Reference)
members of the window object (ever wondered why you can use document.write or XMLHttpRequest directly?)
You don't really need to add new named items to the script engine (you can do so via IDispatchEx if you really want to), there are many ways to execute code using existing named items, for example
the eval function from jscript globals
the execScript function from the window object
the setTimeout function from the window object
You can use one of above as the name of the script method to invoke in HtmlDocument.InvokeScript and pass the code in additional parameters.
Yes - all functions in JavaScript are equally accessible.
Notes
some JavaScript files may not be loaded very early - you need to wait properly (either for document ready/complete, or custom notification from page/simple timeout) till all scripts are loaded.
One can make JavaScript functions "private" - not callable by using window.FuncitionName(...) in JavaScript - you will not be able to call such functions via InvokeScript either.
Sample of such "private" function below:
$(function(){
....
function MyHiddenFunction() {
// you can't call be from outside!!!!
}
});
I found the answer (Something happened and I can't access my account anymore so if someone can answer me that too that'd be swell) but the answer is this:
WebBrowser uses IE7 and IE7 is a piece of crap.
IE9 executes the script just fine.
So I have to force it to use IE9
This was done by using the answer given here:
WebBrowser control and JavaScript errors
So... hooray!
firstly i have searched a lot and all topics seems to be C# : call or invoke a JavaScript function but i want to do the opposite , i want to create a function on C# and also on JavaScript and i want the JavaScript function call the C# function and retrieve it`s data , it seems like a good questions .
The problem is that i have no knowledge on web and i do not know how does it work , but i tried a sample :
Created a class :
public interface IFoo
{
string Bar { get; set; }
}
public class Foo : IFoo
{
public string Bar { get; set; }
}
Then
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public IFoo CreateFoo()
{
return new Foo() { Bar = "somevalue" };
}
public string Bar(IFoo foo)
{
return foo.Bar;
}
}
And Javascript Code :
<script type="text/javascript" language="javascript" >
function Callme(){
alert('Js function start . keep pressing OK')
var foo = external.CreateFoo();
alert(foo.Bar);
foo.Bar = "qwer";
alert(external.Bar(foo));
}
</script>
I get Error from the webbrowser control :
Error : "external" is null or not an object
But the javascript is not showing anything , please guide me if i missed something.
Hold on guys. PAUSE. All of you need to slow down and read. As this guy said:
I get Error from the webbrowser control :
Meaning he is embedding a webbrowser control that opens up this page which runs javascript.
To clear this up, I think he means that:
This is not online.
He has a webBrowserControl in his C# application that opens up a page to run this javascript.
In his app, he wants to use javascript to call a C# method from a class in his app.
Now, I agree that he was a bit ambiguous (hint, please be clearer with your question next time), but you guys are all posting answers and getting ahead of yourself. In fact what he is describing is indeed possible, and this is how you do it.
Now the object you wanna reference in your javascript is window.external. Here is an example in your javascript:
window.external.CreateFoo();
to call CreateFoo(). However, before you can do so, you have to make your class visible to the page that your webbrowser is opening (window.external being the instanced class that you're referencing).
So, to set window.external, when you're creating webBrowser in C#:
webBrowser1.ObjectForScripting = this;
So to sum it up:
In your C# app, set ObjectForScripting of your web browser control to whatever object you want to reference in Javascript.
In the Javascript on your page, you can call window.external.YOURMETHOD(); to call any public method from that ObjectForScripting class that you set originally.
I hope this helps your situation and others will be a bit more careful when reading your question.
EDIT: Also as a reminder, the webBrowserControl depends on the version on IE that the user has installed on his/her computer. So be careful on versioning, javascript will only perform to the extent of what his/her version of IE can handle (same goes for styling and etc.)
Edit Edit: You also need to add the ComVisible attribute [ComVisible(true)]
there is some misunderstood here.
Javascript can't call server function.
Use Ajax for that.
The only way we can think about that is in a framework like GWT, but be careful : GWT compile your java code in javascript which calls via ajax the server.
The thing to have in memory is that if a framework propose you to call a C# or java method directly in javascript, it does not really perform that : it calls a server resource using ajax and then the server resource call the method. The method returns and the ajax response is sent to javascript.
You should really look into how HTTP works and then re-evaluate your question.
You simply can't call a C# function directly from JavaScript because your JavaScript is executed on the client by the browser while the C# is run on the server by the Web server.
If you want to call a function on the server you need to do that through a AJAX request using some kind of Web library like ASP.NET WebAPI or ASP.NET MVC.
To explain Jerome C's answer slightly further.
C# is a server-side language which is executed by a server request. Whereas JavaScript is client-side language, meaning it is handled by the browser. Both have their advantages, and both are commonly used for the same thing, such as validation. The server-side language is a secure way of ensuring something, or executing a command between a database for example. Whereas the client-side language (JavaScript), is more commonly used to improve the user experience but making things more interactive.
As people have suggested, there are ways to communicate between the two, but not "directly". The key word here is AJAX. Using an open source library such as jQuery http://jquery.com/ on the client-side, and then a custom handler (.ASHX file) on the server-side, you can make asynchronous requests between the client and the server.
On a slightly more advanced note; you can directly communicate between JavaScript and C# objects using a library such as SignalR https://github.com/SignalR/SignalR it's a tiny bit fiddly to setup, so I wouldn't recommend it for this instance, but it would definitely be something to consider in the future.
You can't call server function by Javascript directly.
If you need , you can use the __doPostBack("Button1","") instead.
Put your action in the Button1's event.
Javascript and serverside languages cannot directly talk to each other.
The most common (I think) way to do this is using some sort of Ajax call a serverside Web Service:
This means you make a special call in javascript that will reach to a class method written in C#.
Before reading about and making Ajax calls, you should read up on HTTP protocol and how communication between web server and browsers is executed. I think the article in this link is a good place to start reading about HTTP.
You can read about WebServices Web Services related Wiki article, and about Ajax in Ajax wiki Article.
Also :
Ajax calls do not have to be written by hand, javascript frameworks like jQuery can be used. (in jQuery a function like jQuery.$ajax() is very usefull in making ajax calls )
There is an alternative way to do this - you can use a page method.
I have a ASP.NET MVC project and i want to use jQuery on them. as other mention I put the jQuery on head section and found that they will work in firebug.
Now I want to jQuery in my C# class. how i can use them in C# class. I want to run the code but it's never compile where I goes wrong.
public class Manager
{
public static void Test()
{
// i put here jQuery code but they never compiler i try many time.
}
}
what is the right way to use jQuery in C# class. like in javascript code is work if I write but in c# when I want to try something like ajax request.
$.ajax is work fine in javascript but when I want to run them in C# they not compile. What is the right way to send ajax request from c# class.
Please tell me the way I can use jQuery ajax function in c# class.
The main reason you can't is because jQuery is a JavaScript library, not a C# library so it just won't work. However, I'm not sure why you would want to do that.
Your C# code is running server-side, so what does an AJAX request even mean in that context? You are already running code on the server, there is no need to remotely contact the server over HTTP. Just run whatever C# code you need to get the data you want. Using AJAX would be kind of like trying to call yourself on the telephone to ask yourself something.
I think you have a fundamental misunderstanding of how the web works. Javascript (JQuery is just a Javascript library) runs in the browser. Your C# code runs on the server.
The browser makes a request for a page from the server, which is your C# code (well, your code + whatever the ASP.NET MVC framework does for you) which sends a page to the client. This page can include Javascript which the browser executes.
Your server side code doesn't run Javascript (unless you're using a Javascript based server like Node.JS instead of ASP.NET). It can output javascript for the browser to run, but it itself does not run it.
Now as far making a AJAX request from C#, if you're trying to call something on your own site #JohnFx is correct that it would be pointless because you can just call the code directly without making a request.
If you're trying to fetch data from an external site, you can make an HttpRequest from C# as shown here. There may be some wrapper code that makes it easier to work with, but I don't know any off the top of my head (it's not something that's too commonly done). You'll then need to figure out how to parse the response.
JQuery is a javascript library and therefore JQuery code cannot be put into your C# code. In order for your JQuery to run, you must output it with the rest of your html and it will run in the users browser. JQuery is javascript and cannot run on the server with your C# code.
If you'd like to make a web request to a web page from c#, similar to what happens when you do an Ajax request from jQuery, look at the HttpWebRequest and HttpWebResponse classes.
This link is also a good place to start to learn a bit more.
As the other answers point out, you simply can't execute jQuery code from c# (and you shoulnd't) since jQuery is not a technology that's meant to run inside of ASP.NET, on the server. But instead, jQuery is a library of Javascript code that makes it easier to write Javascript scripts, which execute inside the user's browser. Everything that you can do with jQuery, you can also do with pure Javascript since at the core jQuery is just a collection of javascript functions and objects.
well, the jquery is a rich javascript library and javascript is a client side language. It should be used inside the dom.
c# is server side language which is rendered on the server when the request came into server.
You need to undrstand the difference between client side and server side. here is an example article
I have a C# class method that return a xml document not file.
How to read C# method output from javascript???
I don't wont to use script manager.
Any ideas?
A similar Question was asked in this thread
call a vb.net subrouting from a javascript function?
In ajax you can use jQuery, very simple to use if you don't want to use script manager.
You need to be more specific. Are you creating a control that has client functionality or you just need to expose the XML on the client? In the first case you can pass it through a script descriptor, in the second, you can use a web service and call it from javascript. Please provide more details.