Using Component (Creating Basic Component) in ASP.Net: Part 1

Introduction

As we know components enable us to reuse application logic across multiple pages or even say across multiple applications. We can write a method named GetConnection() once and use this method in all the pages consist in website. By taking advantage of components, we can make our applications very easier to maintain, execute, reuse and even extend the use in multiple aspects. But if we are working on simple website project, there is no reason to take advantage of components.

In this article series we will discuss how to build simple and advanced components. I am writing this series in multiple parts. If we are working on Components then we also have to discuss on its methods, properties, constructors, inheritance and interfaces.

Basic Component

In this basic component we will create simple welcome application suing components. Here is the example given below.

Component-in-vb.net.gif

Class1.vb File Code

Imports Microsoft.VisualBasic

Public Class Class1
    Public Function Welcome() As String
        Return "Welcome to MINDCRACKER"
    End Function
End Class

Default.aspx File Code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
  
     <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

       </div>
    </form>
</body>
</
html>

Default.aspx.vb File Code

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim showMessage As New Class1()
        Label1.Text = showMessage.Welcome()
    End Sub
End Class

In the Page_Load() event handler, an instance of the Class1() component is created. Next, the result returned by a call to the Welcome() method is assigned to a Label control. When we open the page in your browser, we'll see the message "Welcome to MINDCRACKER" from the Class.vb File.

Note: Continue in Next Part.

HAVE A GREAT CODING!


Similar Articles