Geeks With Blogs
Joel Ross

Ever since I started writing web apps, I've realized that there's a huge need to be able to write solid Javascript. Even when ASP.NET launched, there was still a huge need to know Javascript - despite the claims you heard. I'm hearing a lot of the same claims now that ASP.NET 2.0 is out - not quite as much, but they're still there, despite the proliferation of AJAX. Obviously, the need for Javascript is not going away.

Well, given that Javascript seems to be such a touchy language, and is dependant (to an extent) on the browser the user is using, why isn't there an extension to the .NET framework that allows us to write our Javascript functions in a language familiar to us, such as C#? It seems to me that it wouldn't be that difficult to manage. The framework already does this for things right now - the Atlas framework handles the differences between the XmlHttp objects in IE and Firefox. Why can't this be extended to allow us to write our own "client-side" methods from the server - and have the framework translate to javascript for me?

Here's an example of what I'm talking about:

public void ChangeText(string text)
   MyLabel.Text = text;

Then on a button:

MyButton.OnClientClick += new ClientMethodEventHandler(ChangeText(MyTextBox.Text));

Obviously, you would have MyTextBox on the page somewhere.

On the client side, this would all get translated. First, the method:

function ChangeText(text)
   document.getElementById('MyLabel').value = text;

This works in most cases, which is good, but what if MyLabel is in another control? By translating on the server side, it would be able to do that for you, so if MyLabel was in a user control (MyUserControl), it would spit out this:

function ChangeText(text)
   document.getElementById('MyUserControl_MyLabel).value = textl

And yes, I know you should check to see if it can find the control first, but I'm just illustrating a point. As for the the client click event, it would render a button like so:

<input type='button' id='MyButton' onClick="javascript:ChangeText(document.getElementById('MyTextBox').value);" Text="Click Me" />

And of course, if MyTextBox is in MyUserControl, it would render as 'MyUserControl_MyTextBox'.

To me, this seems like a much easier way to write client code - for a couple of reasons. First, you're writing it in a language that you are more familiar with, so it'll be easier to write in the first place. Second, you're protected against change. When .NET 5.0 and IE 13 are using JoelScript as the scripting language, your code still works - because the framework generates the client script for you - in the best client script for the task!

Thoughts? Yes? No? Should I send this to the Framework team?

Technorati Tags: | |

Posted on Saturday, March 4, 2006 6:54 PM | Back to top

Comments on this post: Use C# To Write Javascript?

# re: Use C# To Write Javascript?
Requesting Gravatar...
I had the very same idea several weeks ago. Definately send it to ScottG to see what he thinks
Left by Luke Smith on Mar 05, 2006 6:09 AM

# re: Use C# To Write Javascript?
Requesting Gravatar...

I was thinking the same since many months and won't be surprised if Microsoft comes up with such a fantastic feature in .NET 3 or 4.

Left by Kanaiya Parmar on Mar 05, 2006 6:58 PM

# re: Use C# To Write Javascript?
Requesting Gravatar...
If you get a response email to this I would love to hear about it ... email me

Im currently working on a dynamic menu for my site which drops server side variables on the page but because C# cant currently talk to the browser I have to write it in javascript then dynamically write the java to input the correct values for my variables.

if i could just write a simple method in C# on the server then use a sort of runat="client clause" it would be ideal if the framework could implicit convert the code for me based on the browser making the request and a specified language ie javascript !!!

With C# not having any support for the client side execution of code you are very liited by what you can do with your .net applications !!!

The sooner microsoft gets on to this and produces a valid solution the better !!!

what about execution of native C# on the client machine ??? could they not produce classes that cause the browser to call up an on the fly compilation of the script or something ???
Left by Wardy on Jun 07, 2006 1:23 AM

# re: Use C# To Write Javascript?
Requesting Gravatar...
Hii i wana use button of input type="submit" but on click i wana execute C# code .....

could u please help me

thanks in advance
Left by Dhananjay on Aug 04, 2008 5:27 AM

# re: Use C# To Write Javascript?
Requesting Gravatar...
Please take a look at LiveUI javaScript generation it works extactly as you wish.
Left by Alex Ilyin on Nov 23, 2009 11:25 AM

# re: Use C# To Write Javascript?
Requesting Gravatar...
I was wasting ages trying to find out why i was getting 'null'...
my field was inside a ContentPlaceHolder so it was rendered like this:
not nice at all...thats why i try not to use .net framework
Thanks this was very useful
Left by Nicolas on Nov 07, 2011 10:05 PM

# re: Use C# To Write Javascript?
Requesting Gravatar...
function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
for (var i = 0, len = response.routes.length; i < len; i++) {
new google.maps.DirectionsRenderer({
map: mapObject,
directions: response,
routeIndex: i
} else {
$("#error").append("Unable to retrieve your route<br />");

How would it be if this was written in c#?
Left by Meth on Apr 02, 2014 1:51 PM

Your comment:
 (will show your gravatar)

Copyright © Joel Ross | Powered by: