Sabtu, 30 November 2024

OLD Visual Basic .NET

 ASP.NET also supports single-source Web forms. As the name implies, a single-source Web form has its code and HTML stored in the same single file. Many of the ASP.NET code samples and tutorials posted on the Web use single-source files because they are easier to distribute and display. For example, the following single-source Web form calculates the area of a circle:

Visual Basic .NET

<%@ Page Language="VB" %>
<script runat="server">
    Private Sub butCalculate_Click(ByVal sender As System.Object, _ 
        ByVal e As System.EventArgs)
        ' Declare variables.
        Dim dblCircArea, dblRadius As Double
        ' Convert text input to a double (optional).
        If txtRadius.Text <> "" Then _
          dblRadius = System.Convert.ToDouble(txtRadius.Text)
        ' Calculate area.
        dblCircArea = System.Math.PI * System.Math.Pow(dblRadius, 2)
        ' Display result.
        ShowResult(dblCircArea)
    End Sub
   
    Sub ShowResult(ByVal Result As Double)
        litResult.Text = "<h3>Results</h3>"
        litResult.Text += "<p>The circle's area is: <b>" + Result.ToString() _ 
            + "</b>"
    End Sub
</script>
<html>
<head>
    <title>Calculate Area</title>
</head>
<body>
    <form runat="server">
        <h2>Calculate Area
        </h2>
        <hr />
        Circle radius: 
        <asp:TextBox id="txtRadius" Runat="server"></asp:TextBox>
        <asp:Button id="butCalculate" onclick="butCalculate_Click"         Runat="server"
            Text="Calculate"></asp:Button>
        <p>
            <asp:Literal id="litResult" Runat="server"></asp:Literal>
        </p>
    </form>
</body>
</html>

Visual C#

<%@ Page Language="C#" %>
<script runat="server">
    private void butCalculate_Click(object sender, EventArgs e)
    {
        // Declare variables.
        double dblCircArea, dblRadius;
        // Convert text input to a double (optional).
        if (txtRadius.Text != "")
        {
            dblRadius = Convert.ToDouble(txtRadius.Text);
            // Calculate area.
            dblCircArea = 2 * Math.PI * Math.Pow(dblRadius, 2);
            // Display result.
            ShowResult(dblCircArea);
        }
    }
          
    void ShowResult(double Result)
    {
        litResult.Text = "<h3>Results</h3>";
        litResult.Text += "<p>The circle's area is: <b>"         + Result.ToString() + 
            "</b>";
    }
</script>
<html>
<head>
<title>Calculate Area</title>
</head>
<body>
    <form runat="server">
        <h2>Calculate Area 
        </h2>
        <hr />
        Circle radius: 
        <asp:TextBox id="txtRadius" Runat="server"></asp:TextBox>
        <asp:Button id="butCalculate" onclick="butCalculate_Click"         Runat="server"
            Text="Calculate"></asp:Button>
        <p>
            <asp:Literal id="litResult" Runat="server"></asp:Literal>
        </p>
    </form>
</body>
</html>

Visual Studio can edit and even run these single-source Web forms; however, the advanced features like autocomplete are not enabled for any of the code entered between the <script> and </script> elements on the page. For this reason, the code samples in this book are shown as code-behind files.

Tidak ada komentar: