Custom Extended ASP.NET DropDownList


I have been given the task of creating a custom server control to be added to our ASP.NET applications template.  I'm looking to create a custom server control that inherits from System.Web.UI.WebControls.DropDownList to handle the following issue:

ISSUE:  There are several code lookup tables in our databases that have three columns: Code, Description, and Enabled.  The enabled column is to be set to 'N' for Not Enabled when the option should no longer be an available option for the user.  We cannot delete the record due to referential integrity constraints.  We cannot simply return only records from the lookup tables that are marked 'Y' for Enabled because if someone retrieves a previously created record with an option that is now marked 'N', the "Specified argument was out of the range of valid values" exception will occur.  

I am looking for an effecient way to display only enabled options plus the option that the record was saved with if it is not in the list.  I am thinking about returning all records from the lookup tables and storing them in ViewState or Cache and then internally removing all items from the list that have the Enabled column marked as 'N'.  When SelectedValue is set, if the option is not in the Items collection, it will be retrieved from the recordset in ViewState or Cache and programmatically added.

My second idea is to have a read-only textbox overlaying a dropdown (with exception of the dropdown arrow) and to always return the "Code" and "Description" from the main stored procedure.  The textbox will have the text and a custom "Value" property that is set when the dropdown option is selected.  Upon saving the record, the value is always read from the textbox's custom value property.

I'd prefer a custom DropDownList without  a textbox but do not know if it's possible.  Any ideas anyone?

NOTE:  We are still unfortunately on .NET Framework 1.1
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nathan BoveSoftware EngineerCommented:
It sounds like you are making the issue more complicated that it needs to be.  Why don't you simply modify the SQL that populates your dropdown list to include the correct option, something along these lines:

  Enabled = 'Y' OR
  IDField = @IDFieldValue

Open in new window

Then simply pass in as a parameter the ID that the record is currently set to.  This will retrieve all enabled domain values, plus the currently selected domain value.
BLEIPAuthor Commented:
We use stored procedures to retrieve LOVs (List Of Values) that are then added to the application's Cache.  The Cached objects are then retrieved, cast back to a datatable, and bound to the DropDownLists during the inital page load event.  When a form is opened in Add mode, there will not be a record available for the 'OR' portion of your SQL statement.  I suppose I could loop through all DropDownList DataSource tables each time a record is loaded to verify whether the required options are in the collection and if not add them but I was trying to avoid rebinding all DropDownLists every time a new record is selected.  I'm looking for the most efficient way.
Nathan BoveSoftware EngineerCommented:
Given your architecture you really have two options:

1)  Iterate over all of your dropdownlists when they are databound and set their values conditionally as you described.
2)  Revamp your domain table management code.

Personally, I would suggest #2 since your current architecture is somewhat lacking in the ways you have described.  If I were to rewrite it, I would write a class that retrieves all of the domain values for a given domain table, enabled or not.  Then write methods that could retrieve those tables with criteria passed in that would filter out unwanted records from the set.  In this way, you have all of the information you need for any domain table available in the application without having to requery the database.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BLEIPAuthor Commented:
nbove:  Thanks for your responses.  I ended up modifying the stored procs to return all records in each code lookup table (enabled and disabled).  I then have a function that accepts a stored proc name, cache key name, and selectedValue string.  The function executes the stored proc and stores the datatable in Cache and then creates a dataview on the datatable with a filter that retrieves all records that are enabled or match the optional selectedValue parameter.  The only difference now is instead of executing the function that gets the datasource and binds it to the dropdownlist once in the initial page_load event, I am calling it each time the record is retrieved or the form is cleared as the result of being put in ADD mode or a record being deleted.  Thanks for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.