When to use javascript instead of creating an aspx page? - c#

I am new at C#.Net and i have one question that i couldn't manage to find on the internet. When should i use a classic combination of html + javascript + css instead of using an aspx page with a code behind?
As i experienced since i started .net , i found that aspx pages and code behind is a huge ease for developers. I didnt need any piece of javascript code since i started. There must be something wrong. I think i am missing a point. Can you answer my question and tell me some examples that i must use html+javascript+css instead of aspx + aspx.cs or vice versa??
Have a nice day.

Javascript is a client side technology, running only in the browser, whereas ASP.NET runs on the server side. These allow you to achieve different and complementary things.
With a classic server side language, any user interaction that you want to respond to must typically be posted across the internet from the browser to your server. It is then processed by the server, which responds with a new page for the browser to load. This generally means that the response time for the user is slower, though you will have access to a much richer programming environment on the server.
With a client side language, everything is processed on the browser. This allows for faster feedback to the user, though at the expense of working within the much more restricted programming environment that the browser gives you, and with no access to stuff your application may depend on, such as your database.
Of course, the lines are blurred somewhat when you make an AJAX request (usually a call written in Javascript that makes a request to the server, receives the response, and updates the page dynamically).
You mention that you have not used any Javascript so far. Perhaps as a starting point you'd like to investigate validating user input on the client side? This way, errors are caught and reported to the user immediately without the cost of the round trip to the server. http://www.tizag.com/javascriptT/javascriptform.php
Both client side and server side technologies can be powerful and useful. Use a combination of them both to give the best experience for the user.

In my experience, using Javascript/jQuery in .NET has been for UI and client-side validation purposes. If you are building an app that does not require Javascript to meet your client's requirements, then take advantage of what .NET has to offer. However, implementing Javascript is not that hard, so feel free to use what you prefer and is in the best interest of the client. You can still write and use Javascript in an ASPX page.

One of your considerations might well be speed. Javascript in a web-page will run on a site visitor's browser. Code-behind runs on the server hosting the page.

from my experience the main aim to use the companion of html,css,javascript with asp.net when the client needs is for a web app that acts exactly like win app
that u don't need to flush over the page to the server and come back again

Points you are missing
Code behind is not what makes ASP.NET; you can make a web app with all C# code right into the aspx files.
If you choose ASP.NET and C# for a web app, all your pages should be aspx ones, except for very specific and not very common situations.
You need to understand the difference between server side scripting and client side scripting. ASP.NET is a server side scripting technology while javascript is a client side only one. Take a look.
You can create aspx pages that are as simple as you want -even without any relevant serverside scripting- and thats all right.
Attempt to answer
You use the word must, so:
You must use aspx instead of only javascript if you want any kind of server side processing.
You must use an html file with plain javascript, jquery and css if there is the specific requeriment to do so, wich would be very uncommon. That could be a situation where a) the page should be as fast as it can possible be b) you don't mind everyone to be able to see your full code by just selecting view source on the browser c) there is no need at all for server side processing d) you don't mind about the little extra mix of technologies on your web application.

You use Javascript/JQuery to perform operations that does not need any server side processing like validating controls for a range or for empty values, some fancy UI stuff. It is much faster than the code behind because it does not post back to server however you could use UpdatePanel aspx server control to have partial post back and avoid reloading the page.
As a web developer you should always use combination of server-side processing and client-side processing. Working logic and application processes on the client-side allows browser based applications to seem more responsive and to have more "snappiness" to them.

If you are looking for highly customizable and high performance pages, then I would go with html + javascript + css and make calls to some webservice. This way you are not restricted by asp.net controls. Also, there are a lot of caveats with standard out of the box web forms that can lead to page performance issues and overhead - ViewState being one. You can also design your own asp.net controls as well, but there is some learning curve.
It really boils down to personal preference (there isn't anything in one that you can't do in the other) : Fundamentals vs Abstraction. To me javascript has always felt somewhat cumbersome when used in conjunction with webforms, however, with mvc it is a lot more natural, as it would be with a standard html + javascript + css page.

When you want to create static pages you can use html+css+javascript instead aspx.
In case you want things more dynamic you have to use aspx with cs.
For more info go http://www.w3schools.com/aspnet/aspnet_pages.asp

Related

SPA (Single Page Application) Render client or server

I have a simple yet not so easy question. (So, I suppose)
I have a current DurandalJS application in production. I am generating my views using C# Razor syntax. So, I am still using my controller to pass the HTML to the client. Rather complicated to get setup using routes/areas/etc but it has been working.
I am using server side for only one real reason. (other then validation, but we can ignore that for now as I am sure model validation has improved and there are ways to handle this now)
Lets say I have a button. And I only want to show that button on the screen if the user is an admin. Now, of course I can use KnockoutJs (or Auelia/Angular/whatever) to find out the users roles and "hide" the button. But as far as I know, the button is still inside the HTML? (it is in Durandal, not sure if the newer libraries handle this different). So, currently I use server code to do the check then the HTML gets rendered without the button.
Knowing how many people use SPA type libraries, what are most of you doing for simple use cases like this?
Oh, yes, on the server for the return API call, we would have security anyway, so assuming someone activated the button it would not work anyway, I simply do not want my users ever seeing it.

Is C# using any Scripting language for Events?

One of my friend (a php developer) told me that C# doesn't have the capacity for managing events, means events are not generated or not controlled by C# codes (Like Button Click, Selected Index Changing etc). Its by some other scripting language like JavaScript and he told me its JScript. And that all the controls like labels, buttons are client side and doesn't need to go in to server.
Then my question is Why runat="server" for all asp controls? He also told that redirection (Response.Redirect) is also happening in client side, its not server side and C# don't have the ability to do so because its a PROGRAMMING LANGUAGE not a SCRIPTING LANGUAGE.
I am totally confused with his Answer and I am not satisfied with his answer. Can some one tell me what it is?
What is runat="server"? And what if all controls are client side?
Does c# have the ability to Redirect, Event Generation, handling etc?
If c# doesn't have the ability then which scripting language is windows using? And how it works independent to browser?
What about Response.Redirect? Is it client side ? Then how it works?
Does ASP using any Scripting language for events ?
I am new to C#. May be this is poor question but can someone give proper answer? I am totally confused.
By events means, Events like Button_Click,SelectedIndexChanged etc
And i mentioned C# here because i am using C# ,
Thank you
It's not a language thing, it's a framework/technology thing. For example, ASP.net web forms can handle events in the code behind similar to how you would in a windows application, but it's really just wrapping http requests. In MVC (which is also C#), you deal with page "events" by interacting with the controller using your standard http verbs (get, post, put, delete, etc).
I'd tell him that php doesn't handle events either in that case. Since both php and asp are server side languages, they all respond to http requests, not events. But clicks tell the browser to do stuff and communicate with the server. javascript would handle events and then communciate with the server, but the point is, his argument is a bit mixed up.
Other answers:
Runat server just tells asp that you want to use this object on the
server, so it creates some objects and references to html elements
so that you can interact with them in the server side code.
Yes
Windows isn't using a scripting language. Your asp application is
using whichever language you programmed it in. However, ASP.net does generate some javascript in order to emulate windows desktop programming styles.
Response.Redirect happens on the server side, it just tells the framework to build an
object that will translate to a proper HTTP response.
You might want to study up on some of the basics of web development. Specifically how http is statless and how http verbs work. It's easier to understand what the framework is doing when you understand what is happening when you're NOT using a framework.
UPDATE:
After the edits, I re-read your post and I see what the issue is. Your friend is mostly right. C# isn't a client side language, so it can't react to page events -- so he's correct there. However, what ASP.net does is generate javascript that bridges this gap. Conversely, ASP/MVC doesn't do this, so you'd have to write the javascript (or even just basic form submissions) that would talk to the controller, and then you would have to handle the server response accordingly.
So he is right, you can't directly write C# code to respond to page events. But if you develop in C# using ASP.net, it will allow you to code in C# and generate the necessary javascript for you. It'll be mostly seamless from your point of view.
I think perhaps the context of the conversation you had with you friend needs to be understood. It seems you are referring to ASP.net not necessary C# in general.
1. What is runat server And why if all controls are client side
What needs to be understood in the ASP.net world is the difference between design time, server side and client side. In the basic life cycle of a page the compiler would use the design time pages to understand how to construct a control tree on the server side. Controls marked as runat="server" tell the compiler that the control as part of the design needs to form part of the control tree that will get constructed on the server.
2.Is c# have the ability to Redirect,Event Generation,handling etc
Context is important here I suspect we are not talking about C# in general rather ASP.net? ASP.net uses events as the way to interact with requests from the server. When you click on a typical page in ASP.net the client side javascript hooks will catch the onlick event and construct the request to send to the server. The Page will then post back the viewstate, event and event arguments to server. The server side will reconstruct the control tree from the viewstate and based on the event and event arguments raise the appropriate event on the server. If you have setup the server side hooks for the event you will catch them as you would any event. Redirect wouldl also be something that happens in server side code.
3. If c# dont have the ability then which scripting language is windows using ? and how it works independent to browser
C# is not a scripting language it is compiled, well... for the sake of brevity we go with it's compiled. ASP.net pages typically is the combination of both C# server side code and client side javascript code. Cient side would catch events and pass the request onto server side.
4.and What about respose.redirect ? is it client side ? then how it works
Response.redirect would be server side not client side. If you need to redirect to a page client side you would use javascript window.location.href = '...'; or something similiar
Context: Web... otherwise are you sure he/she is a friend :)
As above, it's not "the language", it's your choice of ASP.net flavors
If it's Web Forms, sure, ASP.Net works with Javascript on the client side and whatever .net language on the server side (e.g. c#, vb)
if it's MVC/Razor, there is no dependency. You could completely run everything on the server side via POST, GET, etc. (just sayin'). If one wanted you could do same in Web Forms ... not that you should :)
Conversely, you could just do a 100% Javascript app as well (100% client side) - of course with the understanding of such (limitations, security considerations, etc. etc.)

Listing (in C#) all JS functions binded to client side events of a web page

what I want to do, is to write a program to check if clicking on a links, or buttons in a web page fires a client side code (JS). Also I want, to list all JS functions binded to client side event (onclick for example).
Please help me, thats interesting problem,
Tom.
P.S. I am using C# and .NET 4.0.
I think that the only shot that you have at doing this successfully is to intercept the page as it is being rendered and use something like the HTML Agility Pack to parse the rendered HTML looking for events.
And even that won't cover nearly all of the bases.
We have a tremendous amount of code where javascript function names are generated and assigned to controls dynamically, both on the server side and on the client side, depending on what our needs are.
Trying to infer all of this by just examining code is going to be next to impossible.

Event without postback

In chat scripts new lines adds without reloading page. User's availability status changes without postback. Even in facebook a new comment adds without reloading page. How to fire an event in ASP.NET using C# so that event takes place with reloading the page.
I believe that its not possible using C#. Is there any special library for it in AJAX or jQuery or other?
It's done using native XML components available from the browser when writing JavaScript. These are used to call a server-side page or service to load a block of HTML (or XML) async, and then write it into the document. This is essentially AJAX.
JQuery is a library designed to make writing cross browser compatible scripts easier, and offers built in functionality to help make those AJAX calls for you, without you needing to do the low level bits yourself.
ASP.NET has a range of tools for creating AJAX enabled sites, and recent versions now incorporate jQuery for the client side parts, without you needed to do much wiring up. Together the tools make exposing and calling ASP.NET services really easy. The simplest form is probably the UpdatePanel. It's a good place to start if you have never made an AJAX enabled site before.
Also take a look at the AJAX Toolkit.
jQuery itself allows you to send GET and POST Ajax requests and manipulate with HTML elements
$.post(...
$.get(...

Is possible to develop whole asp.net web form with jquery

Is it possible to develop a whole asp.net web form with jquery ajax instead of pagepostback?
If possible can anyone show some tutorials or examples?
eg binding data to form (textbox, dropdownlist, gridview etc.), send data to server, etc.
Thank you for your support and Sorry if my english is not good
Zabahey
In my experience, if you are doing that much with jquery you'll do much better trying MVC, as this allows you to work much closer to the HTML (important when marrying to jquery) and avoids all the cruft that is meaningless with a jquery-centric app. It is also much easier to write the ajax routes.
But to answer the question: I'm sure that would be possible, but it will be easier without webforms adding work for you to undo.
You can make a web page or should I say simple HTML markup which can use JQuery or any other Javascript Library to load dynamic content from Database. Yes, you can create dropdown's, checkboxes too, but then you have to be very much efficient in Javascript, HTML and DOM knowledge. Because, you can not debug errors rapidly in this case. Also, you need to
understand the asynchronous behaviour of browsers too.
Its just a complete different pool of water to swim in.

Categories