Coding in ASP.NET

Discussion in 'ASP.NET' started by MinalS, Jun 29, 2015.

  1. The Control class is the parent class and all the controls are inherited from it. The ASP.NET file has .aspx extension. It is further divided into sections as code section,page directives, and page layout.

    Page directives

    The Page directives are known as commands. They are used by the compiler when user compiles the web page. The processing of the page is defined by the page directive. The Page directives can be easily added to the web page. The following syntax is used for adding the page directive.

    <%@ [Directive] [Attributes] %>

    The directive is enclosed in “<%@ ….%>”. User can add the directive at the beginning of the web page. Let’s explore different page directives in ASP.NET.

    1. @Page directive

    The @Page directive is used for specifying ASP.NET page attributes. It is used widely in coding ASP.NET application.

    <%@ Page Language=”C#” AutoEventWireUp=”false” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
    2. @Control directive

    The @Control directive helps build ASP.NET user controls. The properties inherited by the user control and inherited values are defined by the user.

    <%@ Control Language=”C#” Explicit=”True” CodeFile=”UserControl.ascx.cs” Inherits=”UserControl” %>
    3. @Master directive

    The @Master directive is used for master pages. The templates page property is defined while using the @Master directive. All the content pages inherit the properties defined in the master page.

    <%@ Master Language=”C#” AutoWireupEvent=”false” CodeFile=”MasterPage1.cs”  Inherits=”MasterPage” %>
    4. @Implements directive

    The @Implements directive provides the pages to be implemented. Only one attribute interface is supported.

    <%@ Implements Interface=”System.Web.UI.IValidator”%>
    5. @Import directive

    The @Import directive is used for importing the namespaces. It supports single attribute and takes string value specifying the namespace to be imported. It does not support more than one attribute/value pair.

    <%@ Import Namespace=”System.Data.SqlClient” %>
    6. @assembly directive

    The @assembly directive attaches the assemblies to the web page. All the classes and interfaces are available. Two directives as Name and src are supported. The Name attribute defines the assembly name and src defines source of the assembly.

    <%@ Assembly Name=”Assembly1” %>
    <%@ Assembly Src=”Assembly.cs”>
    7. @Register directive

    Once the control is added to the web page, the @Register directive is present on the page. The user control is registered on the web page.

    <%@ Register TagPrefix=”Tag1” Namespace=”Name1.Namespace1” Assembly=”Assembly1” %>
    8. @output cache

    The output caching of the ASP.NET page is controlled.

    <%@ OutputCache Duration=”100” VarByParam=”None” %>
    9. @MasterType

    The @MasterType directive connects class name to the page for retrieving the strongly typed references. It supports two attributes as virtualpath and Typename. The virtualpath sets the location for the page. Typename sets the name of the derived class where user gets the types reference.

    <%@ MasterType VirtualPath=”/MasterPage1.master”%>
    10. @Reference

    It declares another page or user control must be compiled and active page or control. It supports virtualpath attribute. The location of the page or user control is set.

    <%@ Reference VirtualPath=”~/Control.aspx”%>

    Page Layout

    The page layout is an interface of the web page. The HTML controls, web server controls, text, JavaScript controls are present.

    The sample code for ASP.NET page layout is as mentioned below:

    <!—directives -->
    <%@Page Language =”C#” %>
    <!—code section -->
    <script runat=”server”>
        private void modify(object sender, EventArgs e)
            string str = txt1.Value;
            modify.InnerHtml = str.ToLower();
            <title>Modify the text</title>
            <form id=”form1”>
                <input id=”txt1” runat=”server” type=”text” />
                <input id=”btn1” runat=”server” value=”Enter Value” OnServerClick=”modify” />

    Visual Studio IDE

    Visual Studio is the Integrated Development Environment through which the developers work for creating programs. The languages like C#, VB.NET, J# are used for programming. Graphical user interface, console applications, WPF and web applications, web services are created using visual studio application.

    There are various elements in visual studio application useful for development.

    1. Solution Explorer Window

    The solution explorer window contains solution name, classes and project. Any file from the explorer can be opened and modified by the user.

    2. Class View

    The class view window shows methods, classes, and properties for a particular file. The window contains buttons for sorting items and folder creation.

    3. Output window

    The status of different features of visual studio application is shown in the output window. Once user compiles an application, the current status is displayed. The errors in the code are also displayed.

    4. Error List window

    The list of errors is shown in the error list window. By double clicking on the error, user can view the error in the code.

    5. Code Editor

    The code editor window helps user to modify the code. The editor is useful for adding code to a class.

    6. Toolbar

    The toolbar has shortcuts menus for various commands. User can add, delete, save, print, load and unload files using menus.

    Consider an example created using Visual Studio application.

    1. Add the following code in the source view window in ASP.NET application.

    <html xmlns=””>
            <form id=”form1” runat=”server”>
            <asp:textbox ID=”txt1” runat=”server”></asp:TextBox>
            <asp:Button ID=”btn1” runat=”server” Text=”Show” OnClick=”DisplayName” />
            <span id=”modify_data” runat=”server”></span>
    2. In the code behind file, add the following code.

    public partial class _Default: System.Web.UI.Page
        protected void Page_Load(object sender, EventArgs e)
        protected void DisplayName(object sender, EventsArgs e)
            string str = txt1.Text;
            modify_data.InnerHtml = str.ToLower();
    3. The output for the code is as shown below:

    Last edited by a moderator: Jan 21, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice