Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 23rd, 2007, 05:07 PM
Authorized User
 
Join Date: Sep 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default The output of ASP.NET driven web pages

Hello,

I've been thinking about learning ASP.NET (2.0) for a few moments now. So, I bought Wrox's Beginning ASP.NET 2.0 with C# and began my journey. And before I could even get my hands dirty there was this one thing that disturbed me.

To say something about my background, I'm not exactly a beginner in web-development. I have previous knowledge of creating web sites using languages such as PHP, Python (with the kind help of Django framework) and even Java.

Now, I'm a bit of perfectionist myself. I just can't stand code which is badly organized and looks like it was written during pretty bad hangover. Not only I want to have neat code in my editor, I also like to have neat output for it. So you can imagine my disgust when I entered the site of imaginary football team Wrox United (http://www.wroxunited.net/), which also happens to be the site you'll build in the book in question, and investigated what was behind the interface I saw in my browser.

So, basically my question is whether the crappy output is a feature of ASP.NET or can it be fine-tuned to be human readable, needless to say, without losing the niceties ASP.NET gives you? Why, for example, does GridView use JavaScript when there's actually no need for JavaScript? Can I still use those "cool" ASP.NET features if I decide I don't want a single line of JavaScript in the output users will see? And finally, what the [insert rude word here] is the meaning of that cryptic string named __VIEWSTATE and can it be removed from the output? I'm sure there is a way, but the question of how hard it will be and what I'm going to lose in the process remains.

Now one might wonder why these things matter. First of all, let's consider a scenario where the user has disabled JavaScript in his or her browser. What happens? Well, basically, 80% of the site functionality gets crippled. That is what I call a problem. And what about bandwidth? The front page is whopping 17.2 KB – and that's without the external style sheets and images. I could make it way under that including images and style sheets. Of course, that really might not be a problem now, seeing their poor performance, but when they manage to crawl up in the league, the bandwidth might cost some serious money as thousands after thousands of fans scan through every bit of information in their favorite teams web-site.

That was pretty much all I wanted to say, so now I'm looking forward on your answers. :)

 
Old September 24th, 2007, 04:46 AM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi there :)

Your first question about __VIEWSTATE. It's hidden input field inside the ASP.NET page's form element. It's function is to act as container for data between page postbacks. It's like session ($_SESSION in PHP) but when session data is available anywhere inside the session, viewstate is only available for that particular page. So when you navigate to another page, viewstate is reset.

ASP.NET enables viewstate and creates that __VIEWSTATE input element by default. This is a little stupid behavior since ASP.NET could detect if there is any data stored in viewstate and only then enable it. But anyway you can disable it by creating web.config and add the following to the config file:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <pages enableSessionState="false"></pages>
  </system.web>
</configuration>

Then your second question about javascript. You can't disable Javascript from ASP.NET. Many controls depend on it more or less. I can only think two ways to overcome this:

1) Use only HTML components
2) Write your own components which doesn't use JS

And 17.2 KB isn't much of a big deal (unless you're on GPRS). You should see some of my projects with huge viewstate ;)

 
Old September 24th, 2007, 03:55 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Since you are not a beginner to Web Development you know full well that HTTP is a stateless protocol and viewstate was created to overcome that limitation to a degree. In Classic ASP for example, if I wanted to maintain the value of a textbox through postbacks I had to do something like:

<input type="text" value='<%=somevariable%>' />

This is cumbersome at best. Viewstate seeks to eliminate that by placing the values of a textbox into that hidden element so the values can be persisted through multiple page postbacks.

Of course, if you are worried about page size and don't need this feature you can disable it as stated above or you can do it on a control by control basis by setting the EnableViewState property to false.

I am confused though "I just can't stand code which is badly organized and looks like it was written during pretty bad hangover. " Are you talking about the code (HTML) that actually renders the page? If you are, I must ask, who cares? For example, if you have a label in your IDE named lblTotalPayments but, when it is rendered, the id is actually something like: plcRoot_Layout_zoneLeft_PrintInvoice_lblTotalPayme nts whats the issue? My only guess would be that you are concerned with overall page size with the extra characters that the runtime adds.

Lastly, if you want the ability to do the cool stuff in .NET without using javascript I am afraid it is more trouble then it is worth. For example a script similar to this is wrote out to all of your pages:

<script language="javascript" type="text/javascript">
<!--
var theForm = document.forms['form1'];
if (!theForm) {
    theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</script>

And this is to enable the posting back of your web forms when a button is clicked. So if you want to not use javascript you will be back to using regular HTML Forms and posting pages the classic ASP way.

All and all, with the hype that has become "Web 2.0" I think you will find that most people surfing the web do not have JS disabled. Of course I can not validate this statement but, then again, you could always use a <noscript> tag to tell the user they need to enable JavaScript to view your website correctly.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old September 24th, 2007, 04:14 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Quote:
quote:Originally posted by weicco
 <?xml version="1.0"?>
<configuration>
  <system.web>
    <pages enableSessionState="false"></pages>
  </system.web>
</configuration>
This has nothing to do with ViewState. This is for session state.

Miko,

Another thing that hasn't been mentioned is that ViewState is vitally important to property control behavior. Consider this example:

You have a page with a textbox on it and a event handler in the page code for the textbox's TextChanged event. On the first page hit, you have a text box that has no value in it. Viewstate contains a value to reflect that. You then enter a value into the textbox and press a button. Your page posts back and the ASP.NET framework reconstructs the state of your last view of the page (hence viewstate) from the __VIEWSTATE field that is also posted. Then it processes the posted form values, which includes the new value you entered into the textbox. It can then determine that you changed the text of the textbox so that it can raise the TextChanged event. If it weren't for viewstate every time the page posted you would get lots of change events firing that shouldn't.

This mechanism is used for radio buttons, check boxes, drop down lists, list boxes and text boxes. Regular buttons which render as input type submit are the ONLY form controls that don't require viewstate for their functionality. This is due to the feature of the HTTP protocol where only the control you took action on has a value in the form collection. This was how we used to determine which submit button you pressed when you had more than one on a page back in ASP days.

Yes, ASP.NET doesn't output the most cleanly formatted HTML. I used to get annoyed by it but I got over it pretty quick once I realized how much easier it was to do everything I needed to do.

You can streamline the output if you dislike what you get from the GridView. If you use repeaters or data lists you get more control over the rendered HTML, but it comes at the cost of not getting many of the additional GridView features (paging, sorting, inline editing templates, etc).

The best advice is to just keep playing with it and read more about how you can fine tune the controls and page to lighten it up.

-Peter
 
Old September 24th, 2007, 05:58 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Quote:
quote:Originally posted by planoie


If you use repeaters or data lists you get more control over the rendered HTML, but it comes at the cost of not getting many of the additional GridView features (paging, sorting, inline editing templates, etc).

This is quite true about losing the features although it is possible, for example, to implement paging with a repeater or a datalist but it comes down to effort vs. return. Can you implement a Datalist with paging, sure, with a bit of custom code you can provide a paged datalist but is it necessary? There are times when, for one reason or another, yes it is necessary so the return out weighs the effort so consider that as well.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old September 25th, 2007, 01:39 AM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by planoie
 
Quote:
quote:Originally posted by weicco
Quote:
 <?xml version="1.0"?>
<configuration>
<system.web>
    <pages enableSessionState="false"></pages>
</system.web>
</configuration>
This has nothing to do with ViewState. This is for session state.
Oh, I meant to write <pages enableViewState="false"> but I was at the moment working with some sessionstate problem so it got me confused :)

EDIT: And if Miko doesn't like to store viewstate to browser it is totally possible to serialize viewstate to SQL Server for example. I can provide details how to do it if necessary.
 
Old September 25th, 2007, 04:01 AM
Authorized User
 
Join Date: Sep 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again and thanks for your answers.

I think I quite got whats the function of viewstates. Now that I think about it, it might prove to be useful at some points. However, if there is a way, as weicco pointed out, to store it in SQL Server instead of hidden input field, I'd probably like to use it that way.

Quote:
quote:
I am confused though "I just can't stand code which is badly organized and looks like it was written during pretty bad hangover. " Are you talking about the code (HTML) that actually renders the page? If you are, I must ask, who cares?
As I said in my starting post, I'm a bit of a perfectionist. It can be translated as "I care". Now, dont reply back with "Cry me a river". What's my concern is that the rendered HTML will mess up (and it does exactly that) the pretty XHTML and CSS I write myself. By doing that, I fear it makes debugging the layout even harder than it is sometimes.

 
Old September 25th, 2007, 07:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there..

you will have no problem debugin layout.. VS is WYSIWYG so you will really build the output layout of the client without any problem.. On your side, you will see very clear code to work with.. On the client side the code will be a little more hard to read, but you will get exactly the same output, even for grids or other controls that you use...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old September 25th, 2007, 07:35 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Quote:
quote:Originally posted by Miko2

What's my concern is that the rendered HTML will mess up (and it does exactly that) the pretty XHTML and CSS I write myself.
I have never run into this problem. Could you give us a specific example of when/how this happens?

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for:
Professional Search Engine Optimization with ASP.NET
Professional IIS 7 and ASP.NET Integrated Programming
Wrox Blox: Introduction to Google Gears
Wrox Blox: Create Amazing Custom User Interfaces with WPF and .NET 3.0
================================================== =========
 
Old September 25th, 2007, 05:27 PM
Authorized User
 
Join Date: Sep 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:
I have never run into this problem. Could you give us a specific example of when/how this happens?
No, I can't give you a specific example. By messing up I meant "it adds something I didn't plan to go there".

Quote:
quote:
you will have no problem debugin layout.. VS is WYSIWYG so you will really build the output layout of the client without any problem..
Eehm, is it really WYSIWYG? If there's one thing I have learned from so called WYSIWYG editors is that what you see is hardly ever what you get. It might look the same on IE, but what about Firefox? Safari? Opera? The list goes on. :)


But, thanks for the answers. Now I'm going back to the book and see what ASP.NET actually has to offer for me compared to other technologies I've used. :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
How to implement unicode in asp web-pages? Swetz Classic ASP Databases 0 May 23rd, 2006 12:17 AM
XSLT in VC++.NET 7.0: Output in Web Browser BigOcean XML 0 February 18th, 2005 11:27 PM
how to upload asp pages to web 222 Classic ASP Basics 1 June 10th, 2003 02:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.