Dailycode.info

Short solution for short problems

On bubble event

When you are creating ASP.net pages and content is build up from a DB, you'll probably work with usercontrols, placeholders ...

Recently I encountered another challenge. I was adding usercontrols to a placeholder to a page dynamically. The user controls have a save button which saved the data to the database. But then the parent page needed to be refreshed. I could do this by using a redirect to the page and it would be refreshed. But when data grows, a page refresh is less and less attractive. So I googled and came up with this solution: The OnBubbleEvent. If you want to read on what the event actually does, please google some more, I can show you a practical example.

Put this code on the page that calls holds the user control.   

    protected override bool OnBubbleEvent(object source, EventArgs args)    
    {
        CommandEventArgs e = (CommandEventArgs)args;        
        if (e.CommandName == "Rebind")        
       {            ClearThePlaceHolders();            
            RefreshTheData();       
        }
        return true;
    } 

   

When you finished the event (After you saved the data) on the user control, you can activate the bubble event!

    CommandEventArgs args = new CommandEventArgs("Rebind", String.Empty);
    RaiseBubbleEvent(null, args);
 

So now, only the part we choose is refreshed. This way the asp stays light weight.