Creating Custom Label Controls
page 5 of 6
by Brian Mains
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27457/ 46

ListLabel Solution

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
        object o = ViewState["EmptyDisplayText"];
        return (o == null) ? "N/A" : (string)o;
        ViewState["EmptyDisplayText"] = value;
    protected override void Render(HtmlTextWriter writer)
      if (!this.DesignMode)
        //Get the selected item
        ListItem item = this.SelectedItem;
        //If the item exists, write out the text value
        if (item != null)
      //Else in design mode, then render specific texts
        //If bound using a data source ID, write that it is bound
        if (this.DataSourceID != null)
        //Else write the ID
          writer.Write("[" + this.ID + "]");

View Entire Article

User Comments

Title: fert   
Name: fo kos
Date: 2012-12-10 8:56:24 AM
fo kos kos ley kos
Title: Nice Addition   
Name: Michael Davey
Date: 2007-11-29 8:46:40 AM
Neat additions to the label control.. thanks!

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-02-27 4:59:41 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search