DEV Community

Cover image for Extending Methods in WebForms Core
Elanat Framework
Elanat Framework

Posted on

Extending Methods in WebForms Core

In this tutorial, we will explain how to extending methods in WebForms Core technology. This allows us to make your code cleaner, more maintainable, reusable, and more responsive to your application's needs.

Here we are using the C# method extension feature; some programming languages ​​may have the same feature as C# or may have other constructs for extending WebForms class methods.

Example 1: Dynamic Message Component

Controller

using CodeBehind;

public partial class AddMessageController : CodeBehindController
{
    public void PageLoad(HttpContext context)
    {
        WebForms form = new WebForms();

        form.Message("Welcome to WebForms Core!"); // Extension method

        Control(form);
    }
}
Enter fullscreen mode Exit fullscreen mode

Extension Method

public static class WebFormsExtension
{
    public static void Message(this WebForms form, string text)
    {
        form.AddTag("<body>", "h3");
        form.SetBackgroundColor("<h3>-1", "lightgreen");
        form.SetText("<h3>-1", text);
        form.Delete("<h3>-1");
        form.AssignDelay(3);
    }
}
Enter fullscreen mode Exit fullscreen mode

What Happens?

  1. Injects an <h3> tag into the <body>.
  2. Styles it with a light green background.
  3. Displays the text, then auto-removes it after 3 seconds.

Example 2: Auto-Generated Table from a Class

Controller

using CodeBehind;

public partial class AppendFormController : CodeBehindController
{
    public void PageLoad(HttpContext context)
    {
        var frameworks = new List<BackendFramework>
        {
            new BackendFramework { Name = "CodeBehind", Owner = "Elanat", ReleasedYear = "2023" },
            new BackendFramework { Name = "Django", Owner = "Django Software Foundation", ReleasedYear = "2005" },
            new BackendFramework { Name = "Laravel", Owner = "Taylor Otwell", ReleasedYear = "2011" },
            new BackendFramework { Name = "Spring Boot", Owner = "Pivotal Software", ReleasedYear = "2014" },
            new BackendFramework { Name = "Express.js", Owner = "OpenJS Foundation", ReleasedYear = "2010" },
            new BackendFramework { Name = "Ruby on Rails", Owner = "David Heinemeier Hansson", ReleasedYear = "2005" },
            new BackendFramework { Name = "ASP.NET Core", Owner = "Microsoft", ReleasedYear = "2016" }
        };

        WebForms form = new WebForms();

        form.GenerateTable("TableBox", frameworks); // Extension method

        Control(form);
    }
}
Enter fullscreen mode Exit fullscreen mode

Class

public class BackendFramework
{
    public string Name { get; set; }
    public string Owner { get; set; }
    public string ReleasedYear { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Extension Method

public static class WebFormsExtension
{
    public static void GenerateTable<T>(this WebForms form, string inputPlace, IEnumerable<T> data)
    {
        form.AddTag(inputPlace, "table");
        form.AddStyle(inputPlace + "|<table>-1", "border-collapse: collapse; width: 100%;");
        form.AddTag(inputPlace + "|<table>-1", "thead");
        form.AddTag(inputPlace + "|<thead>", "tr");
        form.SetBackgroundColor(inputPlace + "|<tr>", "whitesmoke");

        // Create headers using class property names
        foreach (var prop in typeof(T).GetProperties())
        {
            form.AddTag(inputPlace + "|<tr>", "th");
            form.SetText(inputPlace + "|<th>-1", prop.Name);
            form.AddStyle(inputPlace + "|<th>-1", "border: 1px solid #ddd; padding: 8px;");
        }

        form.AddTag(inputPlace + "|<table>-1", "tbody");

        foreach (var item in data)
        {
            form.AddTag(inputPlace + "|<tbody>", "tr");

            foreach (var prop in typeof(T).GetProperties())
            {
                var value = prop.GetValue(item)?.ToString() ?? "";
                form.AddTag(inputPlace + "|<tr>-1", "td");
                form.SetText(inputPlace + "|<td>-1", value);
                form.AddStyle(inputPlace + "|<td>-1", "border: 1px solid #ddd; padding: 8px;");
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Features:

  • Dynamically generates headers using class property names.
  • Iterates through data to create table rows/cells.
  • Applies consistent styling via inline CSS.

The image below shows the result of the table in the example above.
Generate Table in WebForms Core Technology

Example 3: Reusable Form Input Component

Extension Method

public static class WebFormsExtension
{
    public static void AddInputField(this WebForms form, string inputPlace, string label, string inputName)
    {
        form.AddTag(inputPlace, "div");
        form.AddStyle(inputPlace + "|<div>-1", "margin-bottom: 12px;");

        form.AddTag(inputPlace + "|<div>-1", "label");
        form.SetText(inputPlace + "|<label>-1", label);
        form.AddStyle(inputPlace + "|<label>-1", "display: block; font-weight: bold;");

        form.AddTag(inputPlace + "|<div>-1", "input");
        form.SetAttribute(inputPlace + "|<input>-1", "name", inputName);
        form.AddStyle(inputPlace + "|<input>-1", "width: 100%; padding: 8px 0px;");
    }
}
Enter fullscreen mode Exit fullscreen mode

Usage

form.AddInputField("FormContainer", "Username:", "username");
form.AddInputField("FormContainer", "Password:", "password");
Enter fullscreen mode Exit fullscreen mode

Why Use Extension Methods?

  1. Cleaner Code: Encapsulate complex UI logic into reusable methods.
  2. Consistency: Enforce styling/behavior patterns across pages.
  3. Maintainability: Update logic in one place (the extension class).
  4. Readability: Method names describe intent (e.g., GenerateTable).

Conclusion

Extension methods unlock powerful ways to extend WebForms Core’s built-in classes. By creating reusable components like dynamic tables, auto-removing messages, or form generators, you reduce repetition and build more maintainable applications. Start small (e.g., a custom Alert() method), then expand your toolkit as patterns emerge in your projects.

Related links

CodeBehind on GitHub:
https://github.com/elanatframework/Code_behind

Get CodeBehind from NuGet:
https://www.nuget.org/packages/CodeBehind/

CodeBehind page:
https://elanat.net/page_content/code_behind

WebForms Core organization on GitHub:
https://github.com/webforms-core

Warp.dev image

The best coding agent. Backed by benchmarks.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

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

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay