Improve company productivity with a Business Account.Sign Up

x
?
Solved

How can I run a Javascript function inside ASP.net C#?

Posted on 2015-01-12
11
Medium Priority
?
410 Views
Last Modified: 2015-01-23
I'm planning a part of an already existing site (asp.net/c#), where the customer can add things to a shopping basket, but there are rules to what can be put in the shopping basket. If one thing is already in, another item can not be put in. Some items can only be bought, if the customer is younger than a certain age. Prices vary on customer private address. This set of rules, I'd want to code in Javascript, since I can make the browser come up with error messages immediately, when the customer tries something which is not allowed. But, when the customer needs to check out his shopping basket, I would like to do a final check on the items, but this time on the server. (In case the customer changed the javascript code, or the json string that is actually the shopping basket). How can I run the same .js snippet on the server?

I could do it the other way around, making the rules in a C# snippet. Then the client would need to ask about everything in an Ajax call. I would like to minimize the traffic to the web server, so it will only download the whole "store" as a complete web application, and only at the checkout, the server will need to checkout and handle the shopping basket.

Please comment on this.
0
Comment
Question by:lefodnes
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 35

Expert Comment

by:Paul MacDonald
ID: 40544498
The obvious solution is to eliminate/hide/mask those things the shopper can't buy from your catalog.  I would get annoyed if I kept putting things into a basket only to find out I wasn't eligible to purchase them to begin with.  Why show me a product I can't buy?

(That said, I'm sure this is do-able via javascript, but I'm not the guy to tell you how.  Good luck!)
0
 

Author Comment

by:lefodnes
ID: 40544661
That is why I need the rule set. To hide things that are not available after the first item is chosen. This web shop is wizard driven, so the rule set is used by the wizard to know what to ask for next.
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40545410
You can not execute JS in your server code, but something that you could do is to run after the your page is executed in the server:
Alternative 1: Pure JS solution, use jquery $( document ).ready
Alternative 2: If for what ever reason you need to associate this JS code to your server code use ClientScriptManager.RegisterStartupScript

If more help needed please specify OS/VS/ASP.NET version/Project type.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40545431
No, you can't run the javascript validation on the server side ( which is a C# application, right? )

Personally i don't like business rules in the UI module, so what i do is filter the products the client can't buy before sending them to the browser, put simple rules of excluded products in the javascript code and then do a final check in the server side at checkout.

Best regards
0
 

Author Comment

by:lefodnes
ID: 40545540
I'm not closer to a solution, I'm afraid. Isn't there some ActiveX object that could read a js file on the server and execute a function in it? The only reason I want js on the server, is that I can reuse the same code on both the server and the clients. Otherwise, I could just have two equal rulesets, one in c# and one in JavaScript. But it's better to not have to maintain two "equal" functions.

As for the business logic separated from the ui module, I agree, but I would like to take it further, and let the ui module use a _copy_ of the business logic to avoid checking with the server while shopping. Anyway, doing this by Ajax would be the next best solution, as I see it.
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40545571
No, you can't process the javascript in the server side. With an Ajax solution you gain nothing because the server-side is the same.

Check Node.js but i feel this is an overly complex scenario.
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40545627
Duplicate code is not good idea for maintenance purposes. If my previous post alternatives are not applicable to your project, then you should add C# logic in a  web method and called using JQuery post. I know it uses the server but hopefully the information you require to send is not that big.
0
 
LVL 10

Expert Comment

by:Walter Padrón
ID: 40546676
I would like to minimize the traffic to the web server
This is a client requirement or a self imposed one, these days every web application makes a heavy use of ajax calls.

@Miguel i agree that duplicate code is not a good idea, but in different platforms (browser/server, javascript/C#) you will  have some duplicated code but this must be the minimun necessary and TESTS must be in place.
0
 

Assisted Solution

by:lefodnes
lefodnes earned 0 total points
ID: 40546720
It's a self imposed requirement. I'm also running the servers. This web application gets up to 2-5000 unique users per minute, BUT only for a duration of 3-4 minutes. After that, it's half a year until the next time. It's a ticket web shop for a sporting event.

I've found "ClearScript", "JSlint.NET" and a few other alternatives now, after following the node.js suggestion, seeing that node.js works on top of Google Chrome V8 JS engine. Don't know if it is going to work. Trying "ClearScript" first, to see if it can run js-code inside c# ASP.net
0
 
LVL 10

Accepted Solution

by:
Walter Padrón earned 1500 total points
ID: 40547005
In solutions like ClearScript,  you loose the compiler feedback because the code are strings, you can't share the code with the client and IMHO debugging will be a nightmare.

JsLint.Net is a wrapper for JsLint, is a code quality tool not a javascript engine.
0
 

Author Closing Comment

by:lefodnes
ID: 40566028
I had to come up with directions to ClearScript myself. Still not tested, but EE wants me to reply to this question.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Lync server 2013 or Skype for business Backup Service Error ID 4049 – After File Share Migration
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…

595 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question