Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mdogggg 5 posts 35 karma points
    Jun 28, 2013 @ 14:14
    Mdogggg
    0

    Passing data between 2 usercontrols

    Hi there,

    Been struggling with this for a day or so now.  I have a site developed as a standard web app and thought I'd umbraco it.  There is a page where data is pulled from a db and parts of the page are populated from this data.  I split the page up into a number of user controls, thinking I could query the db from one user control and pass the content to the other user controls when the data had been retrieved.

    I can't get this working at all.  I was going to fire events when the data had been retrieved but since the page is in an umbraco template I dont have access to an aspx codebehind to wire the events up. 

    I've just begun looking at the ApplicationBase after coming across it, is this the route I should be taking?

    I'll keep posting my progress but any pointers would be great.

    As a stop gap I could just create a separate template with the whole page defined in a usercontrol, but i would obviously prefer not to do that.

    Thanks,

    Martin

     

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    Jun 28, 2013 @ 15:52
    Lars-Erik Aabech
    101

    There's several options for achieving this.

    From the top of my head, I'd do the following:

    Control that loads from database:

    protected override void OnInit(object sender, EventArgs e)
    {
        var data = LoadSomeData();
        HttpContext.Current.Items.Add("myuniquedatakey", data);
    }

    Postpone binding of data to after OnInit in the other controls, preferably OnLoad:

    protected override void OnLoad(object sender, EventArgs e)
    {
    var data = HttpContext.Current.Items["myuniquedatakey"] as MyDataType;
    if (data == null) // maybe you didn't load the other control, so return if nothing
    return;
    someControl.DataSource = data;
    someControl.DataBind();
    }

    I think that's least effort, max value for now. ;)

    HTH,
    Lars-Erik

  • Mdogggg 5 posts 35 karma points
    Jun 28, 2013 @ 16:16
    Mdogggg
    0

    Thanks for the detailed reply.  I'll have a go and get let you know how I get on :)

    Martin

  • Mdogggg 5 posts 35 karma points
    Jul 08, 2013 @ 16:55
    Mdogggg
    0

    hi there,

    Apologies, was on holiday before I got a chance to implement this.Set it up today and totally works! Just what I was looking for.

    Thanks for that Lars, and for bringing httpcontext to my attention.  Never looked at it much before.

    :)

    Martin

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    Jul 09, 2013 @ 00:11
    Lars-Erik Aabech
    0

    glad I could help :)

  • Mdogggg 5 posts 35 karma points
    Jul 09, 2013 @ 17:50
    Mdogggg
    0

    Hi again, just wondering if you can clear something Lars (or another friendly soul).

    The httpcontext worked a charm on the initial page load to access the data.  I am wondering about custom events. So before I put the site into umbraco I was subscribing to an event in the constructor, using something like:

    myUserControl.OnStatementClicked += new StatementChangedHandler(myUserControl_statementChanged);

    [just to clarify, I had an evaluation.aspx page with two user controls, and I subscibed to the event within the evaluation.aspx.cs page, but in Umbraco you cant access the .cs file of the page holding the user controls to do the subscribing, can you?]

    I am just interested to know how you could do this subscribing in Umbraco?

    Thanks for your time,

    Martin

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 8x c-trib
    Jul 09, 2013 @ 20:24
    Lars-Erik Aabech
    0

    Regarding other controls - you can always search for controls using unique IDs: Page.FindControl("someUniqueId"). IE, if one usecontrol has a panel called "evaluationPanel" and the other "evaluationResult", then you'll find them from the other control by Page.FindControl("evaluationPanel"). OnInit depends on the order they are created in, but they'll be there in OnLoad.

    Regarding events, see if this gives you an idea: http://msdn.microsoft.com/en-us/library/ee817669.aspx.
    Implement the "container" logic in your masterpage (Accessed from a usercontrol by (CastToYourType)Page.Master), or as a singletonish class wrapping the observer list in HttpContext.Items. Then have one controller implement the Observer pattern (listens for events), and the other as a Notifier (sends events). As opposed to the msdn example, the controls would _only_ communicate via the container.
    That is, _if_ you really want them as loosely coupled as you say. It can quickly become messy or confusing if you go too far...

    As said earlier, there's probably dozens of ways communicating between two usercontrols. 90% of them are wrong!

Please Sign in or register to post replies

Write your reply to:

Draft