VB.net Tutorials with Examples

VB.Net Projects

VB.Net Project 1

adplus-dvertising
Life Cycle of ASP.NET
Previous Home Next

Page Life Cycle Events

  • Page_Init: The server controls are loaded and initialized from the Web form's view state. it is basically used for initialized the object and control
  • Page_Load: The server controls are loaded in the page object. View state information is available at this point, so this is where you put code to change control settings or display text on the page.
  • Page_PreRender:The application is about to render the page object.
  • Page_Unload: The page is unloaded from memory.
  • Page_Disposed: The page object is released from memory. This is the last event in the life of a page object.
  • Page_Error: An unhandled exception occurs.
  • Page_AbortTransaction: A transaction is aborted.
  • Page_CommitTransaction:A transaction is accepted.
  • Page_DataBinding: A server control on the page binds to a data source. 

Control in ASP.NET

  • HTML  Controls - Traditional HTML tags
  • Web Server Controls - New ASP.NET tags
  • Validation Server Controls - For input validation
HTML Server Controls
  • HTML server controls are HTML tags understood by the server.
  • HTML elements in ASP.NET files are, by default, treated as text. To make these elements programmable, add a runat="server" attribute to the HTML element. This attribute indicates that the element should be treated as a server control. The id attribute is added to identify the server control. The id reference can be used to manipulate the server control at run time.
Note: All HTML server controls must be within a <form> tag with the runat="server" attribute. The runat="server" attribute indicates that the form should be processed on the server. It also indicates that the enclosed controls can be accessed by server scripts.

Design of Html control

<script language=
"javascript" type=
"text/javascript">
function Submit1_onclick() { 
alert("click me");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Text1" type="text" value="anil" />
<input id="Submit1" type="submit" value="submit" 
onclick="return Submit1_onclick()" /></div>
 </form>
</body>
</html>

Web Server Controls

Web server controls are special ASP.NET tags understood by the server. Like HTML server controls, Web server controls are also created on the server and they require a runat="server" attribute to work. However, Web server controls do not necessarily map to any existing HTML elements and they may represent more complex elements.

The syntax for creating a Web server control is:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
 <div style="width: 246px; background-color: #66FFFF;">
 Name&nbsp;
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 <br />
 <asp:TextBox ID="TextBox2"
  runat="server" ></asp:TextBox>
  <br />
  <br />
  <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
  Text="click me" />
  <br />
  <br />
  </div>
  </form>
</body>
</html>

ASPX CODE

<html xmlns=
"http://www.w3.org/1999/xhtml">
<head runat=
"server">
<title>Untitled Page</title>
</head>
<body>
 <form id="form1" 
 runat="server">
<div style=
"width: 246px; background-color: #66FFFF;">
Name&nbsp;
<asp:TextBox
 ID="TextBox1" runat=
"server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox2"
runat=
"server" ></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat=
"server" onclick="Button1_Click" 
Text="click me" />
<br />
<br />
</div>
</form>
</body>
</html>

VB

Partial Class _Default
Inherits System
.Web
.UI
.Page
Protected Sub Button1_Click(ByVal sender As
 Object, ByVal e As
  System.
  EventArgs) Handles Button1.Click
TextBox1.Text
 = "wiliam"
TextBox2.Text
 = "hello world"
End Sub
End Class

Validation Server Controls

Validation server controls are used to validate user-input. If the user-input does not pass validation, it will display an error message to the user.

Design of validation server control

.ASPX Code

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div style="width: 414px; background-color: #66FFFF;">
        Name&nbsp;
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
            ControlToValidate="TextBox1" ErrorMessage="Blank are not allowed" 
            SetFocusOnError="True"></asp:RequiredFieldValidator>
        <br />
&nbsp;Address&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:TextBox ID="TextBox2"
            runat="server" ontextchanged="TextBox2_TextChanged"></asp:TextBox>
        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="click me" />
        <br />
        <br />
    </div>
    </form>
</body>
</html>

VB CODE

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(ByVal sender As Object, 
	ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "123"
        TextBox2.Text = "hello world"
        Response.Write("hi display textbox 1 value" + TextBox1.Text)
    End Sub
End Class
Previous Home Next