How server side events are called in .NET - asp.net

I am a beginner to .NET, I have some doubts in my mind. Can anybody help me to sort out?
When a user requests for a file(*.ASPX), The request first goes to IIS server and with the help of Handlers and modules it finds the type of file that need to be processed and sent back to the client. But while displaying on the cilent machine the content of the .ASPX file will be displayed as HTML controls. How are the events generated at the client side and sent back to the server?
I know runat=server tells the control will be processed at serverside.
But every time why we need to write "runat=server". Is there any ASP.NET control which runs at client side?

When you tell a "tag" to runat="server", you tell the server to register the tag. Now the tag is included in the control/tag tree on the server, and can be found ont the server: When an event is fired, or attributes or contents is changed server side.
Client side events, are silently converted to a form POST. Together with some extra information, like the ID of the control, and the type of event.
Since your tag is registered on the server, it has an ID, that is also transferred to the client (look at the html source). This tree is build, (by parsing the .aspx file) before the event is "fired". Using this id, you can find the tag in the server-side control/tag tree, and run the method that is used to handle the event.
Using these technique, and a lot of ViewState, Asp.Net tries to deny the stateless nature of HTTP. At first this looks like a good idea, IMHO its better not to fight with nature. Currently ASP.Net MVC feels more naturally.

if you are starting with .NET web development, ASP.NET MVC is the way to go IMO
an ASP.NET server control that is generating client events in the browser is exercising standard dom events via javascript code that gets injected into the page that is sent from the web server. try view page source from any browser to see what is actually generated and the picture will become more clear.
essentially runat=server is telling the ASP.NET parser to process the tag and generate some special HTML output for the page. see GvS's explanation in this thread of how client events for these controls are converted to a form POST that is handled on the server.
http://www.w3schools.com/aspnet/aspnet_intro.asp
How Does ASP.NET Work?
When a browser requests an HTML file, the server returns the file When
a browser requests an ASP.NET file, IIS passes the request to the
ASP.NET engine on the server The ASP.NET engine reads the file, line
by line, and executes the scripts in the file Finally, the ASP.NET
file is returned to the browser as plain HTML
also see
http://www.w3schools.com/aspnet/aspnet_controls.asp
http://www.w3schools.com/aspnet/aspnet_events.asp

These event just raised by server-side controllers ,when you click on a asp button , event information be captured on the client and an event message transmitted to the server, via an HTTP post.i think these possible by some registered event on the server.when you page is loading , some events registered on you page framework , and the raised by http post request.
Look, one of the difference between the client side and server side controls is the server side are registered in server and you can save their state in a view state for example. when you click on a server side button , first its client side event raised and the the server side .note that because the server side controls event registered on the server , when you click on the button ,you page posted back.but the client side controls just are valid in client , and their event just raised for you web browser
need more help ? comment me to edit my answer
regard , Ali

Related

ASP.net dont fire unload on normal postback

If I leave my current page on a asp.net Web application I want that all sessions get destroyed. For that I am using Session.Abandon() on Page_Unload Event. But if I do a Postback with a normal Button_Click I don't want to fire this event.
It would be awesome if you could help me.
Lingo
First you need to understand how the web works. When you access stackoverflow.com for example your are actualy seeing the past. The page you accessed it's already destroyed on the web server.
Based on that principle when you use Page_Unload or Session.Abandon if you actually close your browser you don't send any request for the web server so the web server didn't know (and don't care even) if you close your page.
For doing like
The page unload description act like this it's after being rendered it's had nothing with the close of the page.
"The Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded.
At this point, page properties such as Response and Request are unloaded and cleanup is performed.'
Reference (https://msdn.microsoft.com/en-us/library/ms178472.aspx)
You need to use javascript for doing this behaviour or try the new websocket that will keep the connection alive and the server could check if the client has lost the link with the server.

asp.net how to use sever side changes after post back

In my asp.net application I have a some client side scripting that alters the page events and do different things. One problem I am having is when I post back to the server after all of the validation is done on the client side, I have a couple more validation checks on the server side (c#), and I want to show the proper error, but the page refreshes. I just would like to do some server side coding and if i need to go back to the page, go there as lasted viewed, with the error in the label, and changes i made on server side. Thanks for any help.
I think you must post it in ajaxy fashion.
I mean use ajax to post..
Evaluate the results on client side using JS.
Then if everything is fine do a redirect.
if errors exist you are still on that and can modify the html .
Do your server side validations in an AJAX call instead of a normal page post back. Because, you are making a background server call, your page does not refresh and on validation failure, you can retain the browser side UI. If validation is successful then you can do your regular post-back for actual processing.
From implementation perspective, you can use Page Methods (or JSON WCF services) in ASP.NET AJAX for the same: see this tutorial to get started with Page Methods: http://www.singingeels.com/Articles/Using_Page_Methods_in_ASPNET_AJAX.aspx

HTTP request/response handling untill page life cycle commences

What happens from the point an HTTP request is received on a TCP/IP port up until the Page fires the On_Load event?
The below link should give you detailed explanation about the asp.net application life cycle process.
http://msdn.microsoft.com/en-us/library/ms178473.aspx
Brifely to say.
Request is received by the server, the server determines the ISAPI extension to handle the request based on filename extension.
In case this is first request, it will create an app domain for maintaining isolation with this and other applications running.
then it creates hosting environment which will also create the objects like HttpContext, HttpRequest and HttpResponse.
After this the HTTPApplication object is created .
Afterwards the events in the global.asax which is the class inherited from the HTTPApplication object fires in the order defined in the link above.
The browser recieves the http response
The browser parses http headers and starts reading the HTTP content.
Parsing first the <head> section and parsing this, putting external resources on the get queue (first, css then javascript, ideally)
Parsing the <body> content and drawing elements on the viewport.
When the DOM is drawed to screen and is completely rendered. the page fires the on_load event.
When the HTTP request reaches the server, the server will then prepare the necessary file that is requested by the client and send it to the client. The client will then receive the entire content of the HTML page. Note that this is just the HTML and the browser still needs to make additional requests to the server for images and other types of files like applets if necessary.
Finally, to answer your question, it'll have to depend on where you put the onload event. If it's for the then this will be invoked when the body has completed loading. If it's in other node items, as we call it in JavaScript, then it'll be on the complete loading of that particular item.
Hope it helps :)
Cheers!

Javascript, Ajax, load data from DB dynamically

I have for example ASP.NET site with textbox, button and many other controls.
When user click on button i want to retrieve some data(string,number) from DB(MSSQLServer) and show these data to user without postback(ajax).
How can i do this?
Thanks
There are two key components. A server-side method that can respond to a request for data and a client-side javascript function that can make an AJAX request to that method and consume the data, updating the web page.
On the server side you can use an actual web service or web application methods marked with the WebMethod attribute. These accept a request and respond, not with a web page, but usually a partial HTML snippet, XML, or JSON.
On the client side, I would look at using a framework that implements AJAX -- such as MicrosoftAjax or jQuery, though there are many others. It would be then be a matter of correctly configuring the client function to respond to the interaction trigger (button click) so that it calls the proper method with the right parameters and processes the response.
Partial Page Updates vai AJAX
Using Web Services via AJAX
jQuery -- many examples and documentation

What is the difference between HttpHandler and a Web User Control and when to use each one?

I've been using user controls extensively but never use a HttpHandler and was wondering if I am doing something suboptimal or wrong
Unfortunately your question is a little like "Should I use a sandwich or a cement mixer". HttpHandlers and User controls are completely different things.
HttpHandlers are used to process HTTP requests. For example, if you wanted to dynamically create an RSS feed, you could write an HTTP handler that handles all requests for ".rss" files, creates the output and sends it back to the user.
User controls are used within ASPX pages to encapsulate units of functionality that you want to re-use accross many pages.
Chances are, if you're using user controls successfully, you don't want to use HttpHandlers!
Basically a user control is a piece of server logic and UI. An HTTP Handler is only a piece of logic that is executed when a resource on your server is requested. For example you may decide to handle requests for images sent to your server through your own handler and serve images from a database instead of the file system. However, in this case there's no interface that the user sees and when he visits a URL on your server he would get the response you constructed in your own handler. Handlers are usually done for specific extensions and HTTP request types (POST, GET). Here's some more info on MSDN: http://msdn.microsoft.com/en-us/library/ms227675(VS.80).aspx
Expect a better answer (probably before I finish typing this) but as a quick summary.
A user control is something that can be added to a page.
A HttpHandler can be used instead of a page.
Just to clarify the question. I was reading the Hanselman post
http://www.hanselman.com/blog/CompositingTwoImagesIntoOneFromTheASPNETServerSide.aspx
and thinking that I would never solved the problem with a HttpHandler, maybe with a simple page returning a binary content.
This led me to think that I should add HttpHandler to my developer tool belt.
Even an Asp.Net page is an HttpHandler.
public class Page : TemplateControl, IHttpHandler
A user control actually resides within the asp.net aspx page.

Resources