Click here to Skip to main content
15,896,269 members
Articles / General Programming / Architecture

Binding Web Pages with nHydrate

,
Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
1 Jun 2011Ms-PL12 min read 21K   256   14  
Bind your UI controls to generated objects generically
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acme.EFExample.EFDAL;
using Acme.EFExample.EFDAL.Entity;
using Acme.EFExample.Website.Objects;

namespace Acme.EFExample.Website
{
	public partial class CountryEdit : Acme.EFExample.Website.Objects.BasePersistablePage
	{
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			cmdSave.Click += new EventHandler(SaveData);
		}

		protected override void SetupBindings(EFExampleEntities context)
		{
			int id = this.Request.GetURLInteger("id");
			var item = context.Country.Where(x => x.CountryId == id).FirstOrDefault();
			lblHeader.Text = "Edit Country";
			if (item == null)
			{
				lblHeader.Text = "New Country";
				item = CreateObject(context) as Country;
			}

			this.Mapper.Map(txtName, item, Acme.EFExample.EFDAL.Entity.Country.FieldNameConstants.Name);
		}

		protected override Widgetsphere.EFCore.DataAccess.NHEntityObject CreateObject(EFExampleEntities context)
		{
			//Create a new object and set any default properties when requested
			var item = new Country();
			context.Country.AddObject(item);
			return item;
		}

		protected override void OnDataSavedSucess(EventArgs e)
		{
			//On save success go to the list page
			base.OnDataSavedSucess(e);
			HttpContext.Current.Response.Redirect("/countrylist.aspx");
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)



Written By
Software Developer (Senior) Hewlett Packard
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions