DEV Community

Cover image for Using WebForms Core in Razor Pages
Elanat Framework
Elanat Framework

Posted on

1

Using WebForms Core in Razor Pages

WebForms Core is a new technology for manipulating HTML tags through server-side code, developed by Elanat. This technology is available in all server-side programming languages. This technology is integrated into the core of the CodeBehind framework and you can use this framework under .NET. However, in this article, we want to teach you how to use WebForms Core technology in Razor Pages.

Before we start the article, I must say that Razor Pages has a weak architecture and you can never maneuver it well. Razor Pages has limitations and its flexibility is low.

To use WebForms Core technology in Razor Pages, you need to get the WebForms class on the server and the WebFormsJS library on the client and add it to the project.

The WebForms class on the server can be downloaded from the following link (C# programming language):

https://github.com/elanatframework/Code_behind/blob/elanat_framework/class/WebForms.cs

You can also download the WebFormsJS library from the following link:

https://github.com/webforms-core/Web_forms/blob/elanat_framework/web-forms.js

We added the WebForms class to the Razor Pages project.

We also placed the WebFormsJS library in the wwwroot/script path and added the following script tag in the layout section.

<script src="~/script/web-forms.js"></script>
Enter fullscreen mode Exit fullscreen mode

Note: The examples you will see below are simple; please note that the WebForms Core technology can easily handle the most complex situations.

Example 1: Login

View

@page
@model LoginModel
<form method="post" action="">
    <input asp-for="Username" placeholder="Username" />
    <input asp-for="Password" type="password" placeholder="Password" />
    <input type="submit" value="Login">
</form>
Enter fullscreen mode Exit fullscreen mode

Model

using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc;
using CodeBehind;

public class LoginModel : PageModel
{
    [BindProperty] public string Username { get; set; }
    [BindProperty] public string Password { get; set; }

    public IActionResult OnPost()
    {
        WebForms form = new WebForms();

        form.AddTag("<body>", "b");

        if (Username == "admin" && Password == "123")
        {
            // Store a flag in TempData or Session
            TempData["LoggedIn"] = true;

            form.SetBackgroundColor("<b>-1", "lightgreen");
            form.SetText("<b>-1", "You have successfully logged in.");

            form.Delete("<form>");

            form.AddTag("<body>", "br");
            form.AddTag("<body>", "a", "LoginLink");
            form.SetAttribute("LoginLink", "href", "/Admin");
            form.SetText("LoginLink", "Click on this link to go to the admin page.");
        }
        else
        {
            form.SetBackgroundColor("<b>-1", "red");
            form.SetText("<b>-1", "The username or password is incorrect.");
        }

        form.Delete("<b>-1");
        form.AssignDelay(3);

        return Content(form.Response());
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • form.AddTag("<body>", "b"): Adds a <b> tag to show messages dynamically.
  • SetBackgroundColor() and SetText(): Change UI from server logic.
  • form.Delete("<form>"): Removes form after successful login.
  • AssignDelay(3): Delays the removal of the feedback message by 3 seconds.
  • form.Response(): Sends a response that the WebFormsJS client interprets and applies to the DOM.

The GIF image below shows how the above example works.

Login Example

What does the server respond to the client? (Response after success)

[web-forms]
nt<body>=b
bc<b>-1=lightgreen
st<b>-1=You have successfully logged in.
de<form>=1
nt<body>=br
nt<body>=a|LoginLink
saLoginLink=href||/Admin
stLoginLink=Click on this link to go to the admin page.
:3)de<b>-1=1
Enter fullscreen mode Exit fullscreen mode

Example 2: Event

Because in the Razor Pages framework we cannot add a string from the Model to the View page, we had to add an attribute to the Model class and place it in the View page.

View

@page
@model EventModel
<h3>Move the mouse cursor here!</h3>
@Html.Raw(Model.HtmlContent)
Enter fullscreen mode Exit fullscreen mode

Model

using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc;
using CodeBehind;

public class EventModel : PageModel
{
    [BindProperty(SupportsGet = true)] public bool Change { get; set; }

    public string HtmlContent { get; set; }

    public IActionResult OnGet()
    {
        WebForms form = new WebForms();

        if (Change)
        {
            form.SetBackgroundColor("<h3>", "violet");
            form.SetHeight("<h3>", 200);
            form.SetText("<h3>", "Hello WebForms Core!");
            form.RemoveGetEvent("<h3>", HtmlEvent.OnMouseEnter);

            HttpContext.Response.WriteAsync(form.Response());
            return new EmptyResult(); // Prevent Razor from continuing
        }
        else
        {
            form.SetBackgroundColor("<h3>", "orange");
            form.SetGetEvent("<h3>", HtmlEvent.OnMouseEnter, "?Change=true");

            HtmlContent = form.DoneToWebFormsTag();
            return Page();
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • SetGetEvent(): Adds a client-side event that triggers a new GET request.
  • ?Change=true: On hover, sends query to server to perform update.
  • RemoveGetEvent(): Disables the event after one trigger (one-time interaction).
  • HtmlContent = form.DoneToWebFormsTag(): Injects custom virtual tags into the page.

Please note that when you want to use the WebForms class the first time the page loads, you should use the "DoneToWebFormsTag" method; for responses where the View does not change, you should use the "Response" method.

The GIF image below shows how the above example works.

Event Example

What does the server respond to the client?

[web-forms]
bc<h3>=violet
sh<h3>=200px
st<h3>=Hello WebForms Core!
Rg<h3>=onmouseenter
Enter fullscreen mode Exit fullscreen mode

Note: In the previous example, we definitely needed to request the server to login to the server, and the WebForms Core approach had advanced functionality. In this example, we need a server response to respond to the mouse entry event. However, please note that in WebForms Core technology, we can use advanced features such as "TagBack" and multi-response so that we do not need to request the server.

Conclusion

WebForms Core enhances Razor Pages by:

  1. Simplifying Complex UI Logic: Direct server-side DOM manipulation avoids cumbersome JavaScript.
  2. Event Handling: Server-triggered events replace client-side callbacks.
  3. Dynamic Updates: Modify elements without full-page reloads.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas lets you build and run modern apps anywhere—across AWS, Azure, and Google Cloud. With availability in 115+ regions, deploy near users, meet compliance, and scale confidently worldwide.

Start Free

Top comments (0)

Scale globally with MongoDB Atlas. Try free.

Scale globally with MongoDB Atlas. Try free.

MongoDB Atlas is the global, multi-cloud database for modern apps trusted by developers and enterprises to build, scale, and run cutting-edge applications, with automated scaling, built-in security, and 125+ cloud regions.

Learn More

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay