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

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
    {
      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 + "]");
      }
    }
  }
}

View Entire Article

User Comments

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






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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-18 5:19:12 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search