Adding Widgets ASP.NET

A widget is made up of settings and body parts. The body part is always shown as long as the widget is not minimized. The settings part is only shown when user clicks the “edit” link on the widget header.The settings part stores customization options for the widget.For example, with a Flickr photo widget, settings could include allow the user to choose what type of photos to show, to enter tags, or to enter a user ID.

The settings area hides customization options from the UI until the user wants them,but there can be as many choices as you like. The settings part can be made using a regular ASP.NET Panel that contains all the elements for the customization area. By default, the Panel is invisible, but the widget makes it visible when it is notified that a user has clicked the “edit” link.

As noted earlier, widgets are created as ordinary web server controls.To integrate widget functionality,implement the IWidget interface, which defines how the widget container communicates with the widget(see Example).

IWidget interface

The IWidget interface defines a way to inform the widget when to initialize the widget area and restore its state.When a user clicks the “edit” link, ShowSettings informs the widget to show the settings area. When a user clicks the maximize or minimize links (the plus or minus icons), Maximized and Minimized functions are called.The same happens when a user closes the widget—Closed is called and the widget cleanups any information stored in database. These are all post-event callback methods—actions the user has already performed and the widget reacts to.

IWidget interface

Widgets get references to an interface implementation named IWidgetHost via the IWidget.Init method.This interface exposes methods to communicate with the container as well as the services provided by the container.IWidgetHost allows the widget to consume services provided by the framework,including authentication, notification, and state persistence. For example:

The various methods IWidgetHost uses are as follows:


Stores arbitrary data as XML (or any other format), but because the data needs to be serialized in a string format, XML is the preferred choice.Whatever is stored as the state can be retrieved using the GetState method on the second-time load.


Gets the state that you stored using SaveState.


Maximizes the widget and shows the widget body. It’s the same as a user clicking the “+” button, only the widget does it itself from code.


Minimizes the widget and hides the body area. It’s the same as a user clicking the “-” button, only the widget does it itself from code.


Removes the widget from the page permanently.


Determines whether it’s the first-time load on the page or if an asynchronous postback is happening either on the widget or on some other widget.


The IsFirstLoad property is tricky. Think about what happens when a user clicks some button on a widget and the widget goes through a postback.Basically the whole page is instantiated again and all the widgets are loaded on the server side.Because widgets are user controls, ASP.NET fires the Page_Load method of all the widgets on the page. Now, the widgets need to know if it’s a postback or a first-time

load because the content is loaded from different sources. For example, for a firsttime load, the Flickr photo widget loads the photos directly from Flickr, but on postback it can get the photos from ViewState or some other cache. The IWidgetHost.IsFirstLoad property tells the widget whether it is a first-time or postback load.

You might be wondering, why not use Page.IsPostback, which comes with ASP.NET? It will surely tell whether it’s a postback or first visit to the page.The multi-tab nature of the Ajax web portal redefines what a first-time load is because not all tabs are loaded on the first visit; and widgets on a tab are loaded only when the tab is activated.Imagine a user switching tabs and loading a different set of widgets, but all of the tabs are on the same ASP.NET page. So, when you click on a tab, it’s a regular ASP.NET asynchronous postback to the ASP.NET page. Now you are loading the widgets on the new tab,not on the old tab. If the widgets on the new tab call Page.IsPostback, they will find it true because clicking on a tab is a regular postback for ASP.NET. But for the widgets that are loading for the first time on the new tab, it’s not a postback for them. They will fail when trying to read data from ViewState because no ViewState exists for them yet. This means the user cannot use the regular ASP.NET postback concept for the widgets.This is why the IWidgetHost differentiates regular ASP.NET postback with our own definition of postback for Widgets.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

ASP.NET Topics