Add <ALL> to ReadOnlyList

Add <ALL> to ReadOnlyList

Old forum URL: forums.lhotka.net/forums/t/8625.aspx


dfullbrook posted on Sunday, March 07, 2010

I have a question on the best way to approach using a ReadOnlyList (or NameValueList) with two differing requirements. Let's say I have a list of product Categories. I want to use it in two different ways:

1. As a drop down list when the user adds a new product. He/She can then select only one from the list. No problem.

2. As a drop down list when the user wants to search for a product. In this instance I want to add an "<ALL>" selection at the head of the list, probably with an ID of -1 (which the stored procedure will interpret as ALL).

In the past I've done this in code behind by manually inserting the "<ALL>" selection but I'm sure there's a better way to do this.

DancesWithBamboo replied on Sunday, March 07, 2010

 

Define your drop down like this:

<asp:DropDownList ID="Categories" AppendDataBoundItems="true" runat="server">

     <asp:ListItem Text="<ALL>" Value="-1" />

</asp:DropDownList>

and then bind the control as usual.

 

 

dfullbrook replied on Sunday, March 07, 2010

Thanks, that looks really neat.

dfullbrook replied on Tuesday, March 09, 2010

Thanks, works a treat.

RockfordLhotka replied on Sunday, March 07, 2010

I think the question is whether "all" is a business option or a UI option.

Independent of the UI, would "all" be meaningful? If you were writing a web service, would the caller of the service find "all" as an option, and would they use it?

If so, then "all" should probably be actually in the list - loaded in your data Fetch() method.

If not, then it is a pure UI concern - a convenience for the user - and it should be added to the UI control. The technique for doing that depends on your UI technology and choice of controls.

I didn't know about the ASP.NET solution - that's really cool. I wish similar controls for all UI technologies worked that way!

dfullbrook replied on Sunday, March 07, 2010

I think it probably is just a convenience for the user, but if I were to put it in the Fetch then wouldn't I need two lists?

RockfordLhotka replied on Sunday, March 07, 2010

That's what I'm saying - if it is just a UI artifact you would not put it into the list itself. If it is a UI artifact I think most people would put it into the UI control itself.

Curelom replied on Monday, March 08, 2010

The ASP.NET solution has been around since the beginnning of ASP.NET.  For the life of me, I can't figure out why Microsoft has not ported this powerful technique to the other platforms.

JoeFallon1 replied on Monday, March 08, 2010

dfullbrook

I think it probably is just a convenience for the user, but if I were to put it in the Fetch then wouldn't I need two lists?

Not really. You could simply have two factory methods. One named Fetch and one named FetchWithAll. Then depending on which method is called you can add ALL to the list or not. The rest of the list code is the same so it is just a simple branch in your DP_Fetch code.

 

Copyright (c) Marimer LLC