ASP.NET Tutorial/Language Basics/subroutine

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

asp:Button action event calls a subroutine

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="nfexPage" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="output" runat="server"></asp:Label>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    </form>
</body>
</html>

File: Default.aspx.vb

Partial Class nfexPage
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        output.Text = String.Empty
        DisplayMessage()
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        DisplayMessage()
    End Sub
    Private Sub DisplayMessage()
        Dim i As Integer
        For i = 1 To 4
            output.Text &= "Welcome to my website<br />"
        Next i
    End Sub
End Class


Define and call sub module (function) in asp.net page (VB)

<%@ Page Language="VB" %>
<script runat="server">
   sub Page_Load(Sender as object,e as EventArgs)
      MultiplyNumbers(8,9)
      MultiplyNumbers(4,12)
      MultiplyNumbers(38,23)
   end sub
   sub MultiplyNumbers(intA as integer,intB as integer)
      Response.Write(intA *intB &"<br>")
   end sub
</script>
<html><body>
</body></html>


Pass parameters to subroutine (VB.net)

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="Default" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="output" runat="server"></asp:Label>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>
</body>
</html>
File: Default.aspx.vb
Partial Class Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        output.Text = String.Empty
        DisplayMessage("Welcome to my website<br />")
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        DisplayMessage("Welcome to my website<br />")
    End Sub
    Private Sub DisplayMessage(ByVal message As String)
        Dim i As Integer
        For i = 1 To 4
            output.Text &= message
        Next i
    End Sub
End Class