I have a button
<input type="submit" value="Click Me" onclick="substitute()"/>
In one of my first HTML5 pages. Is there anyway I can write the body of the substitute method in C# code?
Or is it that I have no other option other than JavaScript?
The only way to have C# respond to a web page is to POST the page to a web server, or execute an AJAX request. On the web server side, your request will execute some method that contains C# code.
But it really depends on what you are trying to do; some things are better handled in the browser. Study up on ASP.NET MVC and Javascript, and your strategy will become clearer.
There are some tools that you can use to write code in C# that will be run in the browser. However this requires automatic translation of C# into JavaScript (as only JS is supported by all browsers). This maybe useful if you want to share code in C# between your Desktop app and Web app. The drawback is that any error on the page will be in the auto-generated JavaScript and you will have to somehow map this to your C# code, which may be hard.
Take a look on:
JSIL
Saltarelle
You can find more information in the Miguel de Icaza blog post:
http://tirania.org/blog/archive/2012/Sep-06.html
Like an alternative, I can offer you to look on
TypeScript which is JavaScript language's superscript, with the syntax comfortable for C# developers made by Microsoft.
Very interesting and promising project, but still in active development, as far as I know, so keep an I on regressions and retrocompatibility, if you decide to you use it.
Hope this helps.
Related
In a regular WebBrowser you can click something with this code:
webBrowser1.Document.GetElementById("welcome_box_sign_in_button").InvokeMember("Click");
But in a WebKitBrowser, there is no .InvokeMember. How do I go about doing this?
This is the code for the button I am trying to click:
<input tabindex="5" class="submit spriteall spritesite" id="welcome_box_sign_in_button" onclick="try{}catch(e){};if(!this.elem_welcome_box_sign_in_button){this.elem_welcome_box_sign_in_button=$('welcome_box_sign_in_button');this.spin_welcome_box_sign_in_button=$('welcome_box_sign_in_button_spinner');this.restore=function(t){return function(){t.elem_welcome_box_sign_in_button.show();t.spin_welcome_box_sign_in_button.hide();Event.stopObserving(window, 'unload', t.restore);}}(this);}this.elem_welcome_box_sign_in_button.hide();this.spin_welcome_box_sign_in_button.show();Event.observe(window, 'unload', this.restore);" type="submit" value="Sign In">
I have answered similar question at How to click on a link using Webkit Browser?. And I don't work with Windows and all my experience is on Webkit GTK. Following comments are based on that experience.
Moreover, this is just detailed clarification of answer by gretro.
If your webkit implementation is not supporting DOM APIs, then you can use javascript execution to do lot of work.
Using javascript execution then you can do most, if not all the DOM operations. The API functions are usually same as javascript functions and most of the time call exact same functions internally despite of origination. Communication between your application and javascript can be little challenging, but if you can read alert messages, that also can be solved. It looks like this library does support alert handling mechanism. A tool I wrote at https://github.com/nhrdl/notesMD will show some examples of achieving this communication, though it uses GTK version and is written in python.
Gretro's answer can done as follows. Please watch out for syntax errors as I have not tested this because of lack of access to .NET world. In my experience, statement ending ";" is required by some implementations when executing like this, some complain and some don't care. So that's another thing to watch out for.
browser.Document.InvokeScriptMethod("document.getElementById('<id>').click()", new object[]{});
You could always append a script to the DOM and call it with the InvokeScript method. You could pass the id of the DOM element along with the name of the action to trigger and have Javascript do the rest.
browser.Document.InvokeScriptMethod("functionName", new object[]{"parameter1", "parameter2"});
http://webkitdotnet.sourceforge.net/docs/html/M_WebKit_DOM_Document_InvokeScriptMethod.htm
I'm trying to implement a drag-n-drop fileupload to my website and found a piece of java called Dropzone.js, it holds everything i need . . . i just have no idea of how to use it!
So far I've been programming only in razor ASP.NET (c#) / HTML / CSS, but so far no javascript / jquery.
It's razor webpages, so no mvc.
Due to my current lack of knowledge in java, i apologize in advance but i'm stuck !!
What i'd like to know is:
I'f i've understood things correct, i should not modify Dropzone.js directly, i should use it as a library and integrate it in my other scripts, e.g. in another .js file. Correct?
Any help in this will be greatly appreciated.
Kind regards,
Daniel A. Rischel
.. Edited as per requested.
Well, you're wrong somewhat. It might not be related to the question but please note these scripts are JavaScript and
java != javascript
Got the point? :) Please try to note this in future. Also, Java code cannot be added to these server-side language, because java itself is a language. You can use JavaScript or its library (jQuery) and create some plugins.
I'f i've understood things correct, i should not modify Dropzone.js directly, i should use it as a library and integrate it in my other scripts, e.g. in another .js file. Correct?
True, you need to link this plugin dropzone.js to the site using
<script href="/link/to/dropzone.js"></script>
Then you'll use this script on the whole of the page where it is linked.
And you should never edit the source code of a plugin, untill or unless you know what you're doing. Because you might mess up with the code.
I'd really like to see some source code examples of what a single file drag-n-drop fileupload form would look like. I've googled extensively but not found something i can use yet.
Did you try to read their documentations? They have a good code explaination on the default (main) page of their website too. And really it depends on just the way you style it. Its just a plugin, you will need to add your own CSS code to style the form. This plugin won't style the form for you, all it will do is to handle the upload events and get back with the result. So, which means that form creation and elements is all upto you!
If there is a dummies tutorial in making this work, i'd really like to be pointed in the right direction!
There is no perfect answer to provide you with, however I will provide you with some basics
http://www.javascriptoo.com/dropzone-js
https://stackoverflow.com/questions/tagged/dropzone.js?sort=newest&pageSize=15 (stackoverflow tag)
In the second link, you can go and see what are the very basic of the issues that are handled with what code. Easy methods will be hanlded by you! You'll need some guidance in just the hard jobs. Good luck for that!
My suggestion for you is to first undertand and learn jQuery, then learn how to make ajax calls. After that you'll know how to create and handle the events. The basic code will be
$('input[type=file]').change(function () { // change in value
// send the file via ajax
}
http://jquery.com
This method will be best as you will know what code you're using and what it will do. Using plugin is easy method then this one. But my preference is with the second one; creating your own plugin.
I am using the Nancy Web Framework in my C# Console Application to basically create a Web Administration panel for my software. I have opted to use the Spark View Engine, as it is basically just HTML. I basically want to create a chatbox, except pull the data written to my application's console every X seconds and display it in a box instead.
I have very little experience with JQuery and AJAX, but they aren't overly complicated from the examples I have seen. The issue I am running into is that ALL of the chatbox and shoutbox examples use PHP.
I basically just need something like this...
The only difference is I need to pull the information from my application instead. I can use basic C# methods inside of the HTML (and probably inside of javascript but I haven't tried this). What would be the best way to do this, and are there any examples floating around that don't use PHP?
This was completed using AJAX and JSON.
Well, to use HTML for styling inside some PC program is just not wise. It has much better UI engines, though. But for your information here is nice jQuery shoutbox tutorial, but well, you only need to handle data input and output with C#, so actually I see no problems. The engine which you are using should have some kind of data stream, or requests handler (bla://program/???)
This will probably sound dumb, but I need to execute a C# code from my html file. For example I just want to execute this
System.Diagnostics.Process.Start(#"D:\Movies\HurtLocker.avi");
Not any server side code.
I can't create aspx page, because to open an aspx page in a browser it needs to be hosted in IIS.
You can't execute server side code from a client side page. If your page is aspx you can use a webservice or click a serverside button from javascript.
Edit: If you want to embed a video player please check this link. You don't need server side code for this. You'll be able to do it with javascript.
Where do you want the c# code to run?
If you want it to run in the browser that is being used to render the html then I'd say that was basically not possible. Something, presumably javascript code in the html page, would have to somehow instantiate a .net clr and pass the c# code to it for execution. The clr does have a COM-based hosting interface that would allow instantiation, but even if you could call this from javascript I think that any sensible browser security settings would prevent it.
If you want the c# to run on the server supplying the html page then you should use asp.net.
EDIT
Ok, you want to run it in the browser. I'm not aware of any examples for hosting a clr in the browser process, sorry.
You can create activex/com objects in js using something like var obj=new ActiveXObject("<comclassname>");, and you might be able to create a CLR that way by instantiating one of the COM classes (maybe CLRRuntimeHost) listed on this page. You could then pass your c# code to your clr for execution. More info here and here. I'm really not sure if that would work, though. I've never used the hosting api, I just know it exists!
Seems like an interesting project to try if you are curious, but deploying this in a real environment would likely present lots of problems. Good luck!
Not C#, but how about .NET dynamic language in the browser with Gestalt? http://gestalt.codeplex.com/
You can create a code block in your ASP file but if this is simple HTML file this is not possible.
At least you will have some application that will read the content of the page compile it and execute.
ASP code block
As Pabuc mentioned you can't execute server side code on the client machine in HTML. If you were to use Silverlight you could execute the code client side, but then the client need to have Silverlight installed and it is not strictly HTML anymore.
Silverlight could be used to play movies client side with C#.
The only way to have C# on client side is Silverlight application [update] or any other browser plugin as #kenny mentioned.
I have tried to find a software for this some time. I have software, which has needs, that are hard to make with traditional Web programming.
Now I have made simple demo, how to create HTML5 online application with C# or VB.NET.
It is Scot library which translates C# to Javascript on time when executing .NET application. It also supports events on Browsers, which is executed in c# code.
To original question:
On the Html page you will need to add single line after :
<script src="myclass.cs"> </script>`
to connect .Net class:
using Scot;
//..
public myclass:Document
{
protected override OnConnect()
{
Elements["mybutton"].OnClick+=new JsInputEventHandler(click);
//your initialization //....
}
private void click(object sender, JsInputEventArgs e)
{
Window.Alert("Click()");
}
}
Demos are quite simple, but actually I needed this library for another project.
It would be nice to have any feedback.
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