Click to See Complete Forum and Search --> : New To ASP.NET


mjacobsca
October 13th, 2006, 06:54 PM
Hello,

I'm new to ASP.NET. I've been programming with J2EE for some time now, and I have 5 questions about ASP best-practices and conventions. Thanks in advance for your answers!

1) I've noticed that the web controls cause a post back quite often to achieve dynamic behavior. It seems to me that unnecessary post backs will be problematic and inefficient. Do people try to restrict the post to a single submit button, and code other actions in javascript? I've been using javascript for years, so I am unsure as to how much goes in the code behind page and how much I should keep doing in javascript. Any thoughts here would be appreciated.

2) When creating the web controls, there are pre-defined styles. Is it easy to just attach a class to these controls instead? When I tried this, the class name AND the styles from the control property dialogs were put in the asp tag. What is the best way to handle this? I want my sites to be able to theme the application themselves, so customizability is key here.

3) I think I know the answer to this, but I'll ask anyway. Are variables in the code behind page shared with other calls to the same page? I'm planning on having a base class for my code behind pages, and I want some variables in this class so they are available to all, but I don't want them shared between different requests. My hunch is that they are NOT shared, but I just want to make sure.

4) How much javascript is acceptable with the ASP pages? I'm used to using a lot, and now it seems many behaviors are expected to be placed in code behind. Is the use of javascript considered "old school"?

5) If I want to validate user errors individually for each control, will the built-in validation put all errors in one message box? Or will the user see 5 alerts per page?

Michael

mmetzger
October 14th, 2006, 10:54 AM
Hi - welcome to asp.net - I've found it pretty cool though it does have its own problems (as with anything...)

Before getting to your questions, realize that the answers are quite different depending on the version of .NET you're targeting. If given the opportunity, I would highly recommend using .NET 2.0 as the target - it makes some things "stupid-easy" compared to 1.1 (including authentication, membership, theming, etc.) 1.1 can do all these things, it just requires varying amounts of custom code / etc.

On to the questions:

1) Postbacks work well, but as you guessed, they can cause performance issues. You can disable the "autopostback" feature of any given control / page / etc as needed. Typically, the idea is that if you need server side processing, use a postback. The trick is determining exactly what requires postback. If you're doing validation, a lot of the code is handled for you client side before posting back to the server. Now, you can apply any measure of javascript you want to a control, but the system will automatically generate the required scripts for both postbacks and validation scripts. Best advice - get familiar w/ dealing with postback situations first, then determine where to stop them.

2) If at all possible, use .NET 2.0. The support for CSS (and non-IE browsers) is much better. It also has built in support for themes (aka "skins") as well as templated master pages. If you must use 1.1, I think you have to define everything via the class element - and the browser defines what takes precedence in a control definition.

3)Typically each instance of a page generates a new instance of the class. The caveat to this is with a postback. There are also certain ways to transfer data between pages depending on your need. .NET 2.0 has a new method called a cross-page postback. I'm not sure how this would be handled in regard to your question.

4) Not at all - it really depends on what you're trying to do. Remember that the code-behind is for methods on the server. Javascript is for the client. If you want behavior to occur on the client, Javascript is typically the best way to do it. As mentioned before, the controls will generate some of their own javascript code, but you can attach any custom javascript to the client side attributes of a control. Some examples include mouse over highlighting in a datagrid / etc. Not to mention if using 2.0 with Atlas (aka the AJAX toolkit) the lines blur even further. I wouldn't generate javascript to do what the framework already handles for you but you can add as much as you need.

5) There are different controls for the situation. .NET 2.0 does have a single validation result control that can show a list of all errors in one control. You can also fairly easily emulate this in your own code if need to. You can also have different validators in place on the same control (ie, this is a required field, a field that must have 10-15 alphanumeric characters, and must not already exist in the db. - this would use a requiredfieldvalidator, a regexvalidator, and a customvalidator...)

I'm learning 2.0 now after about 3-4 yrs experience in 1.x. The quality and ease of use is far better... (example - I wrote a custom forms validation control to authenticate with roles against SQL Server. Took several pages of modifications. In 2.0, it's drag & drop and has a better feature set.)

curtis.dehaven
October 14th, 2006, 05:52 PM
I too am fairly new to ASP.NET. And I agree it is pretty cool. Once I got the knack, I am surprised how fast one can get a web/database application up and running.

How does one go from 1.x to 2.0? I'm currently using Dev Studio 2003. Is it just a matter of installing Dev Studio 2005?

Thanks!

Curt

mjacobsca
October 14th, 2006, 05:54 PM
Thanks for the answers. I have one question:

3)Typically each instance of a page generates a new instance of the class. The caveat to this is with a postback.

Please explain your caveat. I see the IsPostBack property is used quite frequently. Is it safe for any particular request to a page?

mmetzger
October 14th, 2006, 10:33 PM
Based on the postback state, the page will retain (certain) settings that it had previously. I was just referencing your question about whether a new instance of a class was used....

mmetzger
October 14th, 2006, 10:34 PM
I too am fairly new to ASP.NET. And I agree it is pretty cool. Once I got the knack, I am surprised how fast one can get a web/database application up and running.

How does one go from 1.x to 2.0? I'm currently using Dev Studio 2003. Is it just a matter of installing Dev Studio 2005?

Thanks!

Curt

Depending on how deep you're into it, pretty much. Creating a new site in VS2005 will automatically target .NET 2.0.