Using the list label control and setting the SelectedValue
or SelectedIndex, switches the appropriate message, making it easy to display
the right message at the right time. One can set a message using the same
construct as any list control:
Listing 3
ucListLabel.SelectedValue = "Intro";
The new message appears when the control is rendered.
The following is a complete code listing, most of which is
shown above:
Listing 4
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Nucleo.Web.Controls
{
public class ListLabel: ListControl
{
public string EmptyDisplayText
{
get
{
object o = ViewState["EmptyDisplayText"];
return (o == null) ? "N/A" : (string)o;
}
set
{
ViewState["EmptyDisplayText"] = value;
}
}
protected override void Render(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if (!this.DesignMode)
{
//Get the selected item
ListItem item = this.SelectedItem;
//If the item exists, write out the text value
if (item != null)
writer.Write(item.Text);
else
writer.Write(this.EmptyDisplayText);
}
//Else in design mode, then render specific texts
else
{
//If bound using a data source ID, write that it is bound
if (this.DataSourceID != null)
writer.Write("Databound");
//Else write the ID
else
writer.Write("[" + this.ID + "]");
}
}
}
}