Web Design that taps into the haromny and vision of your dreams.

ASP.Net for PHP Developers

Guide to ASP.Net for PHP developers

Written By on in C#

1,390 words, estimated reading time 7 minutes.

A quick introduction to ASP.Net for PHP developers. This guide covers the essentials to get up and running with ASP.Net and is aimed at PHP developers.

Active Server Pages (ASP.Net) Series
  1. What is ASP.Net?
  2. ASP.Net for PHP Developers
  3. Creating a Simple ASP.Net Page
  4. ASP.Net Website Navigation Using a SiteMap
  5. Using ASP.Net Master Pages and Content Pages
  6. Validating Input using ASP.Net
  7. Tracing and Remote Debug in ASP.Net
  8. Creating Custom Error Pages with ASP.Net
  9. Managing Session State in ASP.Net
  10. Using Themes and Skins in ASP.Net
  11. Creating User Controls in ASP.Net
  12. Difference between ASP.Net User and Custom Server Controls
  13. Creating Fully Themable Websites with ASP.Net
  14. Ultimate Guide to the Web.Config File
  15. Adding StyleSheets to ASP.Net via C#
  16. Extending the Web Sitemap Xml Document

Firstly let me say that I am a PHP developer on the web. I do not like the ASP method for developing websites, nor do I like the code it produces or the objects it creates. C# and ASP.Net are my professions, PHP is my passion.

Secondly, let's dispel a few myths that are flying around on the Internet and forums:

  • PHP cannot be compared with .Net .Net is an application framework, not a programming/scripting language. A more accurate comparison is ASP.Net and CakePHP or CodeIgniter.
  • .Net is the future of all websites. .Net is the future of Microsoft, however.Net is not just about the Internet. It can be used for Windows development, handheld, smartphones and so on. It integrates easily with existing corporate applications such as SQL Server, Biztalk and so on, thus it has been adopted by many large corporations. Despite this, as of June 2010, Apache/PHP accounts for 54% of the market, while Microsoft only has 25%.
  • PHP is free, .Net is expensive. While PHP and associated technologies are "free software" you still have to pay for hosting. ASP.Net can be written using Visual Web Developer (free) using the .Net framework (free). Hosting costs are usually much higher for ASP.Net enabled accounts though, although the costs are coming down with the introduction of virtualization technologies.
  • .Net supports multiple languages. The .Net framework supports many languages including C#, Visual Basic, Java and even PHP. PHP does not support any other language other than PHP. See #1 above. PHP is the language used to code a website, this is like saying you can only write C# code using C#.
  • PHP does not support Object Orientation. Not entirely true. PHP does support object orientation, however, it is up to the developer to decide whether or not to use it. The .Net Framework will force the developer to use OO.
  • ASP.Net takes more coding than PHP. Yes and No. Some features such as data bound tables can be created using very little code, however, in some areas PHP requires much less code than the same code in C#.

Microsoft Online

Microsoft has a free online course ASP.NET for PHP Developers, however, I found it mostly propaganda about what ASP.Net does better than PHP, how PHP doesn't do this or that, why ASP.Net is better and so on. I found little value in the course as I was expecting "You do that in PHP, In .Net you do it like this...". Hopefully, this guide can provide answers to that question instead.

Microsoft also offers a series of white papers aimed at developers migrating applications from PHP to ASP.Net. The white papers can be found on the Microsoft site.

Introduction

You should have already read the tutorial What is ASP.Net? and be familiar with the C# language.

ASP is very different from PHP. Whereas PHP is a free-form scripting language, ASP.Net is a structured, type-safe framework for constructing web applications. You must think of an ASP page as being an application rather than a web page, it is very similar to developing Windows applications. Additionally, ASP.Net pages have methods and properties in the same way as a Windows form, such as Page_Load, Page_Unload and so on.

In PHP you may use to output a variable. In ASP.Net you can do a similar thing, using <%=variable %>, however ASP.Net provides a set of tools that allow text to be written out using controls, for example, you can use an to outout text in a span and an to outout pure text.

ASP.Net applications are primarily designed within the page designer where you can drag and drop components that will eventually be rendered into the HTML elements. For example, you can add all your controls (tables, literals, images, buttons and so on) in the designer and then from the Code Behind you can call the text or value property of the object to output some data. ASP.Net uses the same form components as a Windows Form, so you will see the label, textBox, dropDownList and listBox as well as some specific ASP.Net controls.

An example of using a label to output string data using ASP.Net:

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

The data can be set in the Code Behind file:

Label1.Text = "Hello World";

This is then rendered in the browser as:

<span id="Label1">Hello World</span>

You can use the same technique on all the other HTML elements, either as HTML elements or ASP.Net controls. For example, say you had a span tag on the page with a name of warningText.

<span runat="server" id="warningText"></span>

Note the tag runat="server". This causes the ASP.Net process to parse this tag and allow the code behind to access it. Without this tag, the compiler will not allow you to use warningText as an object. In the code behind you can use C# to control this tag:

warningText.InnerHTML = "This is a Warning!";
warningText.Style["background-color"] = "red";

This will output to the browser:

<span id="warningText" style="background-color:red">This is a Warning!</span>

Valid XHTML and Browser Compatibility

When using the designer to create an application page it is rendered on the browser the same way in different browsers. ASP.Net handles the rendering differences and serves content based on the user-agent, but only if ASP controls are used. If you manually insert an HTML element you must then manually assign a style to it.

Code that is generated by the editor is fully XHTML compliant as is the CSS, however, there are elements that are not such as the view state. It will not correct any validation errors on elements that you have hand-coded, however, the editor does contain validation tools to help.

Data Presentation

Data in ASP.Net can be presented in a table from a SQL Server data source with no coding at all. The table will be able to have its columns storable, you can insert, delete, select and update records, have multiple pages and so on without any coding. The table can also have themes applied to it from within the page designer.

To add a data table to a page simply drag the table from the Database Explorer to the form. Visual Web Developer will do all the work for you. This is much easier than the PHP+MySql method of writing out table tags with a while loop over all the rows.

Code Control

ASP.Net does have its flaws. As with any WYSIWYG (What You See Is What You Get) HTML editors, Visual Studio and Visual Web Developer will add huge amounts of additional (and often unnecessary) lines to the code generated. ASP.Net also inserts a lot of required data to the HTML, just for it to work properly, such as forms, session and view states.

Debugging

PHP has a very useful var_dump() method for dumping the contents of a variable, array or class object. ASP.Net does not have a similar function, but it does have something which is a) better and b) worse.

If you are running C# in Visual Studio debug mode you can set a breakpoint on the line to analyse, run the code and when the breakpoint is hit you can use the object browser, watches or immediate window to view and/or change values. This is better.

How is this worse you may be asking? Well, you cannot do this on a live server without attaching a remote debugger which to honest is a pain to setup. In PHP it is quite simple to download the script, add a var_dump, upload and refresh the page.

There is no correct language for web development. Personally, I prefer PHP as I have control over what is being sent to the user's browser. I control how things are displayed; I control how the data is generated. While I admit that ASP.Net does have a few advantages, PHP remains my primary choice for web development.

Last updated on: Friday 23rd June 2017

 

Comments
mdrisser

mdrisser

Thank you for a very well written series, this is exactally what I've been looking for.

I too am a PHP developer, have been for the last 11 yrs. I don't believe in locking myself into one programming language/framewrok/style each has its advantages and disadvantages. I code in Perl/CGI, Python/CGI, PHP, Ruby on Rails, and am now setting about learning ASP.NET with C#.

I've read a few too many PHP vs. ASP.NET articles, mostly with fanboys of one or the other bashing the other, or biased towards one or the other, when what I need is "doing 'X' in ASP.NET is equivalent to doing 'X' in PHP", Kudos to you for this series.

Reply to mdrisser

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me