Blog

Using Code-Behind And Inline Code On ASP.NET Web Forms

The basic document type for ASP.NET server-side development is known as a web form. A web form is essentially an enhanced, server-side version of an HTML page. HTML pages use the file extension “.htm” or “.html” while ASP.NET web forms use “.aspx”. The content of web forms is not exclusively server-side: typically, web forms contain a mixture of HTML, CSS and client-side JavaScript code as well as ASP.NET server controls and server-side programming code.

While editing your web forms in Visual Web Developer, you can switch between “Source” and “Design” views or work in “Split” view to see both simultaneously. “Source” view shows you the HTML markup of the page as well as any other code. “Design” view gives an approximate rendering of the document as it will appear in the browser window. However, this is only an approximation, it is still necessary to preview the page in a browser to get a proper idea of how your page is shaping up.

The server-side code associated with a web form can be implemented using one of two models: code-behind and inline. When the code-behind model is used, all server-side code is placed in an external file with a file extension which matches the programming language being used: “.aspx.vb”, if Visual Basic is used and “.aspx.cs” for C#. With the inline model, server-side code is placed in the head area of the web page inside a “script” tag containing a “runat=’server'” attribute.

The Add New Item dialog in Visual Web Developer asks you to choose which model to use on creating the web form. Choose Add New Item from the Website menu; highlight the “Web Form” icon. To use the code-behind model, activate the option “Place code in separate file”. To use the inline model, deactivate this option.

Microsoft recommends using the code-behind model and it has the benefit of separating code from content as well as making debugging easier. Using code-behind can also be useful in collaborative situations, since it allows a web designer to work on an “.aspx” page while a programmer works on the code-behind file. However, many developers still prefer to use the inline model for some, if not all, their ASP.NET websites since they find it simpler and faster to work with a single page.

To learn more about ASP.NET training courses, visit Macresource Computer Training, a UK IT training company offering ASP.NET training courses in London and throughout the UK.

no comment

Leave a Reply