Wednesday, September 15, 2004

The same 2 questions

I spend quite a lot of time loitering about the ASP.NET forums. One of the things I've noticed is that there are 2 issues that come up over and over again:

1) How do I disable the user's back-button/do some action when the user closes the browser/some other client side stuff?

2) How can I lay out my controls/display something in x way/some other HTML thing?

In case anyone actually reads this, here are the answers:

1) There's nothing you can do through ASP.NET that can do client side things. ASP.NET is a server-side technology. As far as your code is concerned, as soon as a request comes in, is processed and goes out, it's dead. It's a server-side, stateless thing. If your user's browser doesn't ask your code to do something, then it won't even know it exists. I may sound like I'm belabouring the point, but this is quite a key thing to Active Server Pages. It works on the server, not the client. "But what about Session and Application state? How can you say ASP.NET applications are stateless?". Well, ASP.NET does a very good job at mimicing state. For example, it uses a SessionID to link web requests with session objects stored on the server. This is one of the reasons why ASP.NET uses cookies all the time. If you configure your app to use cookieless sessions, then you can even see this SessionID in the URL. Once the request is received, if there's a sessionID, either in the URL or the browser cookie, then the server accesses session variables from that. It's not true state, though. It's just imitated.

"Wandering a bit off topic, aren't you?" Well, not really. The fact that everything is done server-side, including state management and EVERYTHING else ASP.NET does is just to help me make my point. ASP.NET knows your browser is there, it even knows a little bit about it (OS version, Browser version etc. from the standard http headers) but that's all. It can't manipulate your browser, it can't disable buttons, there's nothing it can do to the client.

JavaScript can, though. JavaScript is custom-made for this sort of thing. You want a button disabled based on some sort of criteria? Fine JavaScript'll do it. You want a window to pop up and give your user a message? Fine, JavaScript'll do it (although it'd better be something damned worthwile and relevant. There's a special hell for people who create advertising pop-ups)

2) Control and display layout is all done in the browser from the HTML spat out from your ASP.NET application. I'll repeat that. Control and display layout is all done in the browser from the HTML spat out from your ASP.NET application. ASP.NET outputs HTML. No matter what you've got on your web form, no matter what controls you use, no matter whether they're databound controls like DataLists, DataGrids etc. Your application does what it needs to do, and the spits out a load of HTML to the user's browser. As a result, this means that you have a) ultimate power, but b) ultimate responsibility for making sure your controls work and look right. If it's any consolation, it's not too hard. If you can get VB.NET, C# or whatever, as well as knowing what marked-up code (like XML) looks like, you can get HTML.

There's a million and one fantastic resources and tutorials online, just sling "HTML Tutorial" into google and you're away.

Google is a wonderful thing. As is reading. As is MSDN's library.

Apologies for the rant. Just had to get it off my chest.

No comments: