Jawad Khan

Jawad's Lodge - The willingness to torture yourself before others is what makes a developer truly a unique breed.
posts - 45, comments - 149, trackbacks - 155

My Links

News

Archives

Post Categories

Image Galleries

Storing an extra field in an ASP.Net Dropdown List perhaps Primary key with key and value fields .....

In numerous occasions you want to display a drop down box that contains for example a Cit and City No but you also want to have it linked with let's say Employee number since the city list is in a Form where you filling the employee data. the out of box drop down doesn't allow you to store more then 2 fields at a time.

Following solution allows you to store an extra field with the given 2 fields in ASP.Net drop down list.

Usage in .aspx file :

<%@ Register TagPrefix="jawad" Namespace="jawad.Web.WebControls" Assembly="jawad.Web" %>

  <%@ Register TagPrefix="jawad" Namespace="jawad.Web.WebControls" Assembly="jawad.Web" %>

  the control class is in a class library jawad.web.dll you can put it any where you want and put the above registration tag accordingly

   <jawad:dropdownstorespk id="ddCity" runat="server"></jawad:dropdownstorespk>

  Now in the Code behind file to populate the Dropdown list ....

    //-- Bind SortedList to Cities Dropdown
    SortedList extraFields = new SortedList();
    ddClassStatus.DataSource = GetCities(extraFields); //  getCities is a function declared as Sortedlist GetCities(out SortedList extraFields) so this function loads both Key, value pair and extra field using DataReader ...See Data Access implementation at bottom of the post ...
    ddClassStatus.DataTextField = "Value";
    ddClassStatus.DataValueField = "Key";
    ddClassStatus.ExtraField = extraFields;

Now the actual implementation of the Drop Down Control in the Class library ...

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections ;

namespace Jawad.Web.WebControls
{
 ///


 /// Represents a Control that allows the users to select a Single Value from the dropdown.
 /// This control also stores an extra field (may be primary Key) for each item besides Key value pair.
 ///

 [DefaultProperty("Text"),
  ToolboxData("<{0}:DropDownStoresPK runat=server>")]
 public class DropDownStoresPK : System.Web.UI.WebControls.DropDownList
 {
  private SortedList _extraField;
  private string text;
 
  ///
  /// Stores the Extra Field
  ///

  [Bindable(true),
   Category("Misc"),
      Browsable(true),
   DefaultValue(""),
      Description("Stores the extra field as SortedList to DataKeyField")]
  public SortedList ExtraField
  {
   get
   {
    return (base.EnableViewState) ? (SortedList)ViewState["ExtraField"]:_extraField;
   }
   set
   {
    _extraField = value;
    if (base.EnableViewState)
     ViewState["ExtraField"] = _extraField;
   }
  }

  public object GetExtraField(string selectedValue)
  {
   return (_extraField.ContainsKey(selectedValue)) ? _extraField[selectedValue]:null;
  }

  ///


  /// Gets the Key for the Value passed that is stored in Extra Fields Sorted List
  ///

  /// The Value for which the key is required
  /// The Key that corresponds with the value passed
  public object GetExtraKey(string Value)
  {
   return _extraField.GetKey(_extraField.IndexOfValue(Value));
  }


  ///


  /// Render this control to the output parameter specified.
  ///

  /// The HTML writer to write out to
  protected override void Render(HtmlTextWriter output)
  {
   base.Render (output);
  }
 }
}

Here is the Sample implementation of ReadData that can be called from GetCities Function that also read extra field from the database for the Dropdown list ... This is only sample code that used custom Data Access layer but can simply be implemented with outofbox ADo.Net ...

  public SortedList ReadData(string procedureName, SortedList ExtraField, SqlParameter[] parms )
  {
   SortedList lookupCollection = null;

   using (SqlDataReader reader = SqlHelper.ExecuteReader(_connectionString, CommandType.StoredProcedure, procedureName, parms))
   {
    // Always call Read before accessing data.
    lookupCollection = new SortedList ();
    
    while (reader.Read())
    {
     // Assummimg the first field in the select statement is Key and the next one is value
     lookupCollection.Add (reader.GetString(0),reader.GetString (1));
     if ((ExtraField != null) && (reader.FieldCount > 2))
      ExtraField.Add (reader.GetString(0), reader.GetString (2));
    }
    reader.Close ();
   }
   return lookupCollection;
  }

 

Print | posted on Tuesday, June 21, 2005 10:09 AM | Filed Under [ ASP.NET ]

Feedback

Gravatar

# re: Storing an extra field in an ASP.Net Dropdown List perhaps Primary key with key and value fields .....

This is an aweful lot of work for something so damn simple!!!!
8/31/2005 8:07 AM | _
Gravatar

# re: Storing an extra field in an ASP.Net Dropdown List perhaps Primary key with key and value fields .....

how can i implementate this libray?
1/29/2007 5:43 PM | Roberto
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 

Powered by: