ASP   «Prev  Next»


Lesson 12Course Project Interactive Message
ObjectivePut an interactive message into the course project.

ASP Course Project - Interactive Message

The last few lessons have provided you a number of programming techniques that can put interactivity into an HTML Web page by inserting one or more lines of ASP.net code.

To use Blazor on ASP.NET for data manipulation and logic-based processing (constants, variables, totals, branching, loops, and type checks), you rely on C# code within Blazor components. Below is a breakdown of how to accomplish each of the five objectives in Blazor using best practices.
  1. Create and Store Constant or Variable Data
    • Blazor uses C# for logic. You define variables and constants inside @code { } blocks of Razor (.razor) components.
    • Example:
      @page "/data"
      

      Constant and Variable Demo

      Sales Tax Rate: @TaxRate

      Product Name: @productName

      @code {
          private const decimal TaxRate = 0.07M; // constant
          private string productName = "Gaming Keyboard"; // variable
          private int quantity = 2;
          private decimal price = 59.99M;
      }
      
  2. Calculate Totals and Percentages
    • Use C# arithmetic operations and format them in Razor:

      Subtotal: @(price * quantity):C

      Tax: @((price * quantity) * TaxRate):C

      Total: @((price * quantity) * (1 + TaxRate)):C

    • Or encapsulate logic in a method:

      Total: @CalculateTotal():C

      @code { private decimal CalculateTotal() { decimal subtotal = price * quantity; return subtotal + (subtotal * TaxRate); } }
  3. Branch and Loop
    • Branching:
      @if (quantity > 5)
      {
          

      Bulk discount applies.

      } else {

      No discount.

      }
    • Looping:
        @foreach (var item in shoppingCart) {
      • @item
      • }
      @code { private List<string> shoppingCart = new() { "Mouse", "Keyboard", "Monitor" }; }

  • Test Data Types and Values
    • Use is, typeof, or nullable checks:
      @code {
          private object data = "Hello Blazor";
      
          private string TypeCheck()
          {
              if (data is string)
                  return "It's a string!";
              else if (data is int)
                  return "It's an integer.";
              return "Unknown type.";
          }
      }
      

      @TypeCheck()

    • Value comparison:
      @if (price == 59.99M)
      {
          

      Price is set correctly.

      }
  • Manipulate Numeric, Character, and Date Data
    • Built-in Methods:
      @code {
          private DateTime today = DateTime.Now;
          private string upperName = "blazor".ToUpper();
          private double sqrtValue = Math.Sqrt(25);
      }
      

      Today: @today.ToShortDateString()

      Name Uppercase: @upperName

      Square Root of 25: @sqrtValue

    • User-defined function:
      @code {
          private string FormatProduct(string name, decimal price)
          {
              return $"{name} - {price:C}";
          }
      }
      

      @FormatProduct("Webcam", 79.99M)


  • ✅ Summary Table
    Task Syntax Example
    Constant private const int MaxQty = 10;
    Variable private string name = "Blazor";
    Total Calculation subtotal = qty * price;
    Branching (`if`, `else`) if (qty > 5) { ... }
    Looping (`foreach`, `for`) foreach (var item in list) { ... }
    Type Checking if (obj is string)
    Built-in Functions DateTime.Now, Math.Sqrt(x), string.ToUpper()
    User-defined Function private string Format(...) { return ... }



    Don't forget, though, that the purpose of all these techniques is to allow your Web site to respond dynamically to different users, navigation paths, and events.
    The pages of our Course Project site are pretty much static at this point, so our first Project exercise will add some interactivity to the Home Page in the form of a message about a sale that changes as the sale ends.
    If you have not yet downloaded, uncompressed, and stored them in a virtual directory at this point, please take the time to do so before continuing on to the exercise.

    Course Project - Exercise

    Click the Exercise link below to add an interactive announcement to your Course Project home page.
    Course Project - Exercise
    The next lesson concludes this module.

    SEMrush Software