Page component with code behind (VB.net) : Introduction « Custom Controls « ASP.NET Tutorial






<%@ Page language="VB" %>
<%@ Register TagPrefix="java2s" TagName="Calculator" src="Control.ascx" %>

<html><body>
   <form runat="server">
      <java2s:Calculator id="Calc1" runat="server"/>
   </form>
</body></html>

File: Control.ascx

<%@ Control Inherits="CalculatorControl" src="Control.ascx.vb" %>

Number 1: <asp:textbox id="tbNumber1" runat="server"/><br>
Number 2: <asp:textbox id="tbNumber2" runat="server"/>
<asp:button id="btAdd" runat="server" Text="+" 
   OnClick="btOperator_Click" />
<asp:button id="btSubtract" runat="server" Text="-" 
   OnClick="btOperator_Click"/>
<asp:button id="btMultiply" runat="server" Text="*" 
   OnClick="btOperator_Click"/>
<asp:button id="btDivide" runat="server" Text="/" 
   OnClick="btOperator_Click"/>
The answer is: 
<asp:label id="lblMessage" runat="server"/>

File: Control.ascx.vb


Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class CalculatorControl : Inherits UserControl
   public lblMessage as Label
   public btAdd as Button
   public btSubtract as Button
   public btMultiply as Button
   public btDivide as Button
   public tbNumber1 as TextBox
   public tbNumber2 as TextBox
   
   Sub btOperator_Click(Sender as Object, e as EventArgs)
      lblMessage.Text = Operate(CType(Sender, Button).Text, _
         tbNumber1.Text, tbNumber2.Text).ToString
   End Sub
   
   private function Operate(op as string, number1 as string, number2 as string) as double
      select case op
         case "+"
            Operate = CInt(number1) + CInt(number2)
         case "-"
            Operate = CInt(number1) - CInt(number2)
         case "*"
            Operate = CInt(number1) * CInt(number2)
         case "/"
            Operate = CInt(number1) / CInt(number2)
      end select
   end function
End Class








14.1.Introduction
14.1.1.Differences between user controls and web pages
14.1.2.Page control with full qualified name(C#)
14.1.3.Get value from asp.net page control (VB.net)
14.1.4.Use properties defined in custom control
14.1.5.Page component with code behind (VB.net)
14.1.6.Exposing Properties from a User Control
14.1.7.Set the properties from user control programmatically.