Giter VIP home page Giter VIP logo

webforms.binding's Introduction

WebForms.Binding

This is a tiny abstraction to enable full model binding with the old ASP.NET web forms, in case you're unfortunate enough to still be working with those.

It provides a fast low-level interface for generating input names, and an easier high-level interface that's likely considerably slower.

It's not quite as easy as model binding under MVC, but it at least enables you to use web forms as a templating language for better HTML and JavaScript integration, and lets you get rid of all of that viewstate.

Example View Models

I'll use the following view models in the examples below:

public class PresentationViewModel
{
    public int Id { get; set; }
    [Required]
    public DateTime? PresentationDate { get; set; }
    public RegistrationViewModel[] Attendance { get; set; }
}
public class RegistrationViewModel
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    public bool? Present { get; set; }
}

The code behind for the .aspx might look like:

public partial class PresentationView : System.Web.UI.Page
{
    protected PresentationViewModel model;

    protected override void OnLoad(EventArgs e)
    {
        // load the view model from some backing store
        var id = Request.QueryString["id"];
        model = GetDataStore().LoadPresentationViewModel(id);

        // apply any updates via model binding on every postback
        if (IsPostBack)
            TryUpdateModel(model, new FormValueProvider(this.ModelBindingExecutionContext));
        base.OnLoad(e);
    }
    
    protected override void OnPreRender(EventArgs e)
    {
        DataBind();
        base.OnPreRender(e);
    }
}

So the page loads a pre-existing view model for a scheduled presentation. After load, any registered event handlers are run, and then the whole page is data bound on pre-render. I've found this pattern to work fairly well for most scenarios.

High-Level API

We can bind to it as follows:

<h1>My Presentation</h1>
<div><input name="<%# model.BindTo(x => x.PresentationDate) %>" type="datetime" value="<%# model.PresentationDate?.ToString("yyyy-MM-dd") %>" required /></div>

<h2>Attendance</h2>
<asp:Repeater runat="server" DataSource="<%# model.Attendance %>" ItemType="MyProject.RegistrationViewModel">
    <ItemTemplate>
        <label>
            <input type="hidden" name="<%# model.BindTo(x => x.Attendance[Container.ItemIndex].Id) %>" value="<%# Item.Id %>" />
            <input type="text" name="<%# model.BindTo(x => x.Attendance[Container.ItemIndex].Name) %>" value="<%# Item.Name %>" required />
            <input type="checkbox" name="<%# model.BindTo(x => x.Attendance[Container.ItemIndex].Present) %>" value="<%# Item.Present %>"
                <%# Html.Checked(Item.Present == true) %> onchange="__doPostBack()" />
        </label>
    </ItemTemplate>
</asp:Repeater>

Low-Level API

The low-level API provides maximum performance but is more verbose. Given the same classes as above, using the low-level API looks like:

<h1>My Presentation</h1>
<div><input name="<%# Input.Name[nameof(model.PresentationDate)] %>" type="datetime" value="<%# model?.ToString("yyyy-MM-dd") %>" required /></div>

<h2>Attendance</h2>
<asp:Repeater runat="server" DataSource="<%# model.Attendance %>" ItemType="MyProject.RegistrationViewModel">
    <ItemTemplate>
        <label>
            <input type="hidden" name="<%# Input.Name[nameof(model.Attendance)][Container.ItemIndex][nameof(ChildEntity.Id)] %>" value="<%# Item.Id %>" />
            <input type="text" name="<%# Input.Name[nameof(model.Attendance)][Container.ItemIndex][nameof(ChildEntity.Name)] %>" value="<%# Item.Name %>" required />
            <input type="checkbox" name="<%# Input.Name[nameof(model.Attendance)][Container.ItemIndex][nameof(ChildEntity.Present)] %>" value="<%# Item.Present %>"
                <%# Html.Checked(Item.Present == true) %> onchange="__doPostBack()" />
        </label>
    </ItemTemplate>
</asp:Repeater>

Each component of the path needed to access the child element must be added to the name, which you can do via indexing integers or property names. You can use the nameof() expression so the name used corresponds exactly with the code's property name.

webforms.binding's People

Contributors

naasking avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.