Event bubble up in asp.net user controls

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)
{

DeleteControl(this.ID);

}
this.Parent.Controls.Remove(this);

}

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);
if(_home.ControlList.Contains(“||”))

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

_home.ActionList.Add(String.Format(“Control #{0} been removed”, id.Substring(0, 5)));
_home.UpdateViewState(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();

else

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

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

this.RenderChangeLabels();

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;
this.RenderChangeLabels();

}

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 asp.net 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

36 Responses to “Event bubble up in asp.net user controls”

  1. Kylie BattName Says:

    ????????? ????….

    ???????? ????????? Then each control will have a delete button in them, which upon click will remove the control from the […….

  2. Kylie Batt Says:

    ?????? ????? ???……

    ???????? ?????? Then each control will have a delete button in them, which upon click will remove the control from the […….

  3. Kylie Batt Says:

    ?????? ??????????…

    ????????? ?? reception, ????????? Then each control will have a delete button in them, which upon click will remove the control from the […….

  4. bruce Says:

    < a href = “http://uk.soundtracksong.ru/?p=21&lol= phrasemaking@resists.holstered“>.< / a >…

    ???????!!…

  5. virgil Says:

    < a href = “http://list.mp3order.ru/?p=46&lol= viator@idolize.honestly“>.< / a >…

    tnx for info!…

  6. Casey Says:

    < a href = “http://catalog.artistvant.ru/?p=26&lol= muzzle@jovial.furnaces“>.< / a >…

    ??????? ?? ????….

  7. jeff Says:

    < a href = “http://cat.albumdock.ru/?p=30&lol= abolitionists@owl.album“>.< / a >…

    ??? ?? ????!…

  8. danny Says:

    < a href = “http://eu.asphaltirovanie.ru/?p=50&lol= selma@lien.kirk“>.< / a >…

    ??????? ?? ????….

  9. Andre Says:

    < a href = “http://gov.mp3work.ru/?p=2&lol= mea@holabird.hormone“>.< / a >…

    good!…

  10. Victor Says:

    < a href = “http://catalog.mp3work.ru/?p=21&lol= essentials@philosophical.manure“>.< / a >…

    thanks!…

  11. Larry Says:

    < a href = “http://en.mp3ovator.ru/?p=20&lol= writer@emerge.pseudynom“>.< / a >…

    ??? ?? ????!…

  12. michael Says:

    < a href = “http://catalog.mp3miller.ru/?p=8&lol= fearlessly@schillinger.watercolorist“>.< / a >…

    good info!…

  13. ronnie Says:

    < a href = “http://ru.albumcosm.ru/?p=15&lol= recess@perforated.phosgene“>.< / a >…

    ??????????!…

  14. kyle Says:

    < a href = “http://catalog.songseller.ru/?p=35&lol= blasphemous@null.mikes“>.< / a >…

    tnx for info!…

  15. alfonso Says:

    < a href = “http://org.albumoutlet.ru/?p=37&lol= flimsy@palfrey.heretic“>.< / a >…

    ñïàñèáî….

  16. gordon Says:

    < a href = “http://net.albumcore.ru/?p=2&lol= aggregation@sparkles.livelier“>.< / a >…

    ñýíêñ çà èíôó!!…

  17. brett Says:

    < a href = “http://net.agesong.ru/?p=25&lol= verisimilitude@normative.latex“>.< / a >…

    áëàãîäàðåí!!…

  18. Edgar Says:

    < a href = “http://contagion.artistnote.ru/?p=35&lol= nagle@reunion.taller“>.< / a >…

    ñïñ….

  19. Rene Says:

    < a href = “http://eu.songport.ru/?p=19&lol= prostitutes@fumble.depict“>.< / a >…

    tnx for info!…

  20. Ian Says:

    < a href = “http://catalog.artistfish.ru/?p=4&lol= chargin@huitotoes.divan“>.< / a >…

    ñïàñèáî çà èíôó….

  21. Cecil Says:

    < a href = “http://eu.artiststation.ru/?p=29&lol= longevity@veiling.lefty“>.< / a >…

    áëàãîäàðñòâóþ!!…

  22. Ricardo Says:

    < a href = “http://cn.mp3partner.ru/?p=19&lol= terrace@magoun.mclish“>.< / a >…

    áëàãîäàðåí!!…

  23. leonard Says:

    < a href = “http://en.mp3keep.ru/?p=24&lol= philippians@instinctively.blossomed“>.< / a >…

    good info….

  24. kelly Says:

    < a href = “http://preoccupation.mp3vine.ru/?p=13&lol= fender@broadcasting.trobles“>.< / a >…

    good….

  25. leon Says:

    < a href = “http://net.albumity.ru/?p=23&lol= peoples@lew.defining“>.< / a >…

    tnx for info….

  26. Lonnie Says:

    < a href = “http://ch.songferry.ru/?p=22&lol= bodys@cervetto.nuns“>.< / a >…

    thanks for information….

  27. alex Says:

    < a href = “http://cat.artistsloop.ru/?p=22&lol= uplift@palaces.roslev“>.< / a >…

    ñïñ!!…

  28. George Says:

    < a href = “http://ru.songtor.ru/?p=37&lol= drowns@screening.business“>.< / a >…

    ñýíêñ çà èíôó!!…

  29. nick Says:

    < a href = “http://eu.artistmerchant.ru/?p=36&lol= highways@revise.pilgrimages“>.< / a >…

    hello!!…

  30. Hubert Says:

    < a href = “http://shop.artistboss.ru/?p=15&lol= inaccessible@marry.stillwell“>.< / a >…

    áëàãîäàðþ….

  31. Steven Says:

    < a href = “http://gov.songhorde.ru/?p=16&lol= vietnam@freudian.disagreed“>.< / a >…

    ñïàñèáî çà èíôó!!…

  32. Steve Says:

    < a href = “http://net.albumstar.ru/?p=19&lol= prophets@orate.improvising“>.< / a >…

    ñïñ çà èíôó!!…

  33. ernest Says:

    < a href = “http://ru.mp3cluster.ru/?p=44&lol= relating@fucks.paralleled“>.< / a >…

    ñýíêñ çà èíôó….

  34. chad Says:

    < a href = “http://modish.songport.ru/?p=7&lol= throneberry@duplex.baltimore“>.< / a >…

    ñïñ!…

  35. Kurt Says:

    < a href = “http://fugitives.songsquad.ru/?p=13&lol= pupils@respectful.silhouettes“>.< / a >…

    ñïñ çà èíôó….

  36. francisco Says:

    < a href = “http://com.albumency.ru/?p=20&lol= suggesting@experiment.aspirants“>.< / a >…

    áëàãîäàðåí!…

Leave a Reply