Easily create Drag & Drop dashboards in ASP.NET

by Jan Blomquist 5. February 2009 10:20

In the previous version of Gaia Ajax we introduced the sample above. It served as a great starting point for creating dashboard solutions, but we got a lot of feedback on the poor readability of the sample. In the upcoming release of Gaia Ajax 3.5, we're refactored the example completely to make it much more reusable in terms of easily adding your widgets to the containers. Hopefully you will be able to use the code out-of-box to create dashboard solutions for your users. We've also made the chrome around the widgets optional and configurable. That means you don't have to use ExtendedPanel as a chrome around your widget. Maybe a simple Panel will do ?

Many online providers today offer dashboard options for their customers. IGoogle, Netvibes and Pageflakes to name a few. I guess the comparison stops there, because thanks to Gaia Ajax we were able to create the desired functionality with less than 200 lines of code of C#. That's mostly because of the powerful building blocks we offer in the library. 

Now let's walk through some code showing you howto add your custom widgets to the dashboard. Just by populating the Widgets Array and specify a default container you have done most of your part of the job. Of course you have to create the widgets too, but that's as easy as creating a control and we'll show you an example of that. 

   1:  private readonly Widget[] widgets = 
   2:  new Widget[]
   3:  {
   4:     new SearchWidget("search", "Search", WidgetContainer.Left),
   5:     new AdWidget("ads", "Ad", WidgetContainer.Left),
   6:     new ImageGalleryWidget("gallery", "Image Gallery", WidgetContainer.Center),
   7:     new ScratchPadWidget("scratch", "ScratchPad", WidgetContainer.Center),
   8:     new NewsWidget("news", "News", WidgetContainer.Right),
   9:     new OpenWindowWidget("window", "Open Window", WidgetContainer.Right),
  10:     
  11:  };

 

Without furher delay, let's move to the construction of a widget for the dashboard. We're going to create the Ajax Image Gallery and then we define three members including the ImageButton. Then we use the InitializeComponent function to set default properties and initialize the controls. In the click handler of the ImageButton we circulate through the items in the collection. Notice the clever use of the "properties of remainders" to loop through the items. 

 

   1:  public class ImageGalleryWidget : ChromedWidget
   2:  {
   3:      private readonly Label _lit = new Label();
   4:      private readonly Panel _panel = new Panel();
   5:      private readonly ImageButton _imgGallery = new ImageButton();
   6:   
   7:      public ImageGalleryWidget() { }
   8:      public ImageGalleryWidget(string id, string caption, WidgetContainer container) : 
base(id, caption, container) { }
   9:   
  10:      protected override void CreateChromeControls()
  11:      {
  12:          _panel.Controls.Add(_imgGallery);
  13:          _panel.Controls.Add(_lit);
  14:          ChromeControls.Add(_panel);
  15:   
  16:          base.CreateChromeControls();
  17:      }
  18:   
  19:      protected override void InitializeComponent()
  20:      {
  21:          _panel.ID = "imgGallery";
  22:          _panel.Style["padding"] = "10px";
  23:          _panel.Style["padding-bottom"] = "25px";
  24:   
  25:          _imgGallery.ID = "imgWidg";
  26:          _imgGallery.Click += imgGallery_Click;
  27:          _imgGallery.Style["float"] = "left";
  28:          _imgGallery.Style["padding-right"] = "10px";
  29:          _imgGallery.Style["margin-bottom"] = "15px";
  30:          _imgGallery.ImageUrl = string.Format("media/contentImages/jazz_albums/{0}", 
musicImg[ImgNo]);
  31:   
  32:          _lit.Text = "Click image to browse Image Gallery";
  33:          _lit.ID = "clickText";
  34:   
  35:          base.InitializeComponent();
  36:      }
  37:   
  38:      private int ImgNo
  39:      {
  40:          get
  41:          {
  42:              if (HttpContext.Current.Session["JazzImgNo"] == null)
  43:                  return 0;
  44:              return (int)HttpContext.Current.Session["JazzImgNo"];
  45:          }
  46:          set { HttpContext.Current.Session["JazzImgNo"] = value; }
  47:      }
  48:   
  49:      void imgGallery_Click(object sender, ImageClickEventArgs e)
  50:      {
  51:          ImageButton img = sender as ImageButton;
  52:          ImgNo = (++ImgNo) % musicImg.Length;
  53:          img.ImageUrl = string.Format("media/contentImages/jazz_albums/{0}", 
musicImg[ImgNo]);
  54:      }
  55:   
  56:  }

 

Because you can load ANY control in the framework, it should be easy for you to load ASCX controls dynamically if you want to provide those. We've really put a lot of effort into simplifying the samples and the code reductions for the IGoogle sample was more than 60%. Less code == Less errors Laughing

If you want to extend the sample further, here's a couple of ideas for you. 

  • Create a plugin framework that automatically loads modules into the dashboard
  • Create a persistence layer that load/save the layout configuration.
  • Create different chromes around the widgets. 

Comments

4/26/2009 6:46:40 AM #

Thank you these will help a lot with my review in preparation for the test.

itcomputerzone

4/30/2009 4:59:35 PM #

I tried to run this code but got some error.actually i missed the semi colon.Now its working fine.thank you for the script.I am using it now in my website

Programmable Thermostats

5/5/2009 11:36:18 AM #

thanks for sharing, its really helpful..

custom essay

5/5/2009 11:38:49 AM #

Hi, I am creating a web part page and when I drag-and-drop a web part from one zone to another (there are 3 zones), sometimes the web part stays in the new zone and when I go to move another web part, the previous web part I moved also moves to a different zone.

Term Paper Help

5/5/2009 11:39:21 AM #

It's crazy and I can't seem to figure out why this is happening. I downloaded the Microsoft ASP.NET Futures (July 2007) Release. There is a script manager on the page, and the web part manager, and web part zones are all within the ContentTemplate of an UpdatePanel. I was having the same problem, however, even before I updated to enable drag-and-drop in Firefox. Thanks in advance..

Dissertation

5/5/2009 11:39:58 AM #

I am trying to design a user friendly dashboard where the user can drag ServerControls/UserControls to any Zone they like on the screen, then serialize / deserialize the whole dashboard for each user. This way users will be able to customize the various canned zones that we offer and customize the content of each zone even.

thesis help

5/5/2009 11:40:31 AM #

My whole challenge for the time being is implementing the drag and drop of server controls inside the zone, and serialize / deserialize the new layout per use. Can someone show me how to tackle this interesting project? and suggestion will be highly appreciated.

research paper help

5/12/2009 5:56:13 AM #

I was not knowing that this was possible.I will try it out and let you know wether it worked for me or not.

Film

5/12/2009 5:58:10 AM #

It worked for me.I never thought its gonna be so easy.Thanks a lot for this lovely tutorial.

True Refrigeration

5/13/2009 5:28:16 AM #

I want to say very thank you for this great tutorial

Bisnis Online

5/13/2009 1:49:35 PM #

Thanks for the great reference post.

md5 hash

5/17/2009 11:48:51 AM #

Interesting post, this was really useful. thanks!

melissa

Comments are closed