Archive for the ‘Journal’ Category

Event bubble up in user controls

Sunday, April 6th, 2008

As I was new in MVP and NMock, I was given a small assignment to code. The goal was to be able to add user controls dynamically in an aspx file by clicking an “Add control”. Then each control will have a delete button in them, which upon click will remove the control from the page. There was also one more thing, upon changes in the user control, each change will appear in a panel in the Default page.

Now it sounded pretty easy in the beginning. But to implement things in the MVP architecture created the first barrier. It was hard to put the mindset first on MVP. To take the control away from the web project made the event handling a bit tricky for me. There are actually 2 type of event which needed communication between the dynamically created user controls and the parent aspx page –

  • Upon deletion let the aspx page know which control was deleted, and update the view state. And do this by the Presenter.
  • Upon change in content of a control, let the aspx file know to update the change log.

The first event was handled in the following way-
( Code in the ascx.cs file ( the View) )

public delegate void DeleteControlDelegate(string id);
public event DeleteControlDelegate DeleteControl;

protected void btnDelete_Click(object sender, EventArgs e)

if (DeleteControl != null)




Now the Presenter for the Parent aspx page has a method with the same signature of the DeleteControl(id), UpdateViewState.

public void UpdateViewState(string id)

home.ControlList = _home.ControlList.Replace(id,String.Empty);

_home.ControlList = _home.ControlList.Replace(“||”, “|”);

_home.ActionList.Add(String.Format(“Control #{0} been removed”, id.Substring(0, 5)));


Now who to hook up the two? Thats actually the easy part. See, when I added the control dynamically in the aspx page I hooked it up as follows –

public string AddControl(string type, string id, bool isNew)

lblMsg.Text = String.Empty;
DemoModule uc = (DemoModule)Page.LoadControl(Constants.PATH);
uc.Type = type;
uc.DeleteControl += new DemoModule.DeleteControlDelegate(_homePresenter.UpdateViewState);
uc.PropagateAction = this.Show_Activity;
if (isNew)

uc.ID = System.Guid.NewGuid().ToString();


uc.ID = id;pnlControl.Controls.Add(uc);

this.ActionList.Add(String.Format(“Control #{0} has been added.”, uc.ID.Substring(0,5)));


return uc.ID;


The second event was rather easy. As I didnt have to bubble up dynamic information from the user control, just the action was bubbling up, I used the systems event handler delegate to work for me ( this.Show_Activity ).

protected void Show_Activity(object sender, EventArgs e)

Control ctl = (Control)sender;
string controlName = ctl.GetType().Name;
string text = _homePresenter.FormatMessage(controlName,”change”);
this.ViewState[Constants.CHANGELOG] = this.ViewState[Constants.CHANGELOG] + “|” + text;


Its the simplest solution. But its not complete, if we want to bubble up what was changed, the specific information, we would use the first custom delegate. Even still the code is messy, there is too much code and decisions in the code behind file. To clear it up, I could create an object that will emulate the user control. Presenter will update the object and view will just show it. But that looks like too much work for something so small. But as I now know, its quite easy if needed :).

the sample solution is here

Some more reading on Dependency Injection and MVP

Wednesday, March 12th, 2008

In my new office I was studying the following articles.


I came to read these when I started to study about Unit Testing on ASP.NET projects. And here all use MVP. Its all new experience for me.
I have always been a fan of NTiers applications pattern. But now I am required to work on MVP with Feels little out of my normal brainwave, but its stimulating. Stepping out of my comfort zone, thinking architectures in a sort of reverse way, or top-down and bottom up in parallel. Currently the architectures I have managed to put up of Dependency Injection is through constructor injection. I am loving how pluggable and traceable the solution is becoming using MVP.

I am trying to explain or map MVP from a NTierian’s point of view. Lets say a solution have 4 projects (DAL, BLL, Facade and website). In NTiers Website will communicate with Facade. Facade will communication with BLL and website. BLL will communicate with DAL and facade. In NTier BLL handles the business logic with Data layer. Website’s ASPX pages handles the UI related business logic themselves in their code-behind files.

Now say if I am to translate the design in MVP. There is no 101 relation there. See the website will be called the ‘VIEW‘ but it will not handle the business logic related to the population of the GUI, a ‘PRESENTER‘ will do it with the help of the interfaces for each view ( Factory method). Now ‘MODEL‘ will act as the facade here who will handle all sorts of other logics, both Presenter and DAL have communication with it. Good to know that the DAL will always be DAL, no matter what framework it is in.

The work flow of MVP and MVC from Nikola Malovic

Actually I don’t know if I am right or wrong. Its just 3 to 4 days I am playing with MVP and this is what I felt. I am a complete beginner in t, so if anyone is reading do leave your view on it, and correct me if you find me wrong. It will be highly appreciated. My MVP readings will be found at Delicious.

I am working on a sample project where I am using MVP. I will write about it once i am done :).

For shortnote there are 3 types of dependency injection – Constructor Injection(I use it for NMock Unit testing my presenters), Setter Injection(Spring.NET framework), and Interface Injection(Avalon). I am still not wise enough to shade some lights on those. I will do that someday.

Things to cover (and i am done)

Tuesday, February 19th, 2008
  • advance function of sql2005
  • the new sql syntax and those syntax related functions of sql 2005
  • caching(the framework cacheing) and state management
  • user control and their event life cycle
  • asp .net page life cycle
  • MVP wih and user controls.

I was planning to cover these within a month along with my new job and I am happy for myself I did a bit more than what i wished for. But i am still lagging behind framework caching.

What am I doing?

Thursday, November 29th, 2007

I am doing a whole lot of things. Preparing for MCTS ( actually playing with visual studio 2008), trying out the facebook apps, trying to do something productive ( @#*!?). And also doing some soul searching in 2.0, asp ajax and universal widget API. Still not sure which one will I pursue but enjoying all of those.