user3437721
user3437721

Reputation: 2289

restricting ASP.net dropdown

I have to restrict the content of some dropdown boxes in ASP.NET.

There are 6 dropdowns on the page, and the dropdown items get populated from the database.

In the database, the dropdown table is like this:

ID    Text
1     This is some text
2     Some more text
3     Even more text
4     This is just silly now

So for each textbox on the webpage - I want to display only certain database table rows.

For example, for dropdownbox1 I might want to display table IDs 1 and 2. For dropdownbox2 I might want to display table IDs 2 and 3.
For dropdownbox3 I might want to display only row 4.

So without hardcoding this in LINQ, would I be better creating another table for each dropdown box, and what IDs I want to display? I'm not sure whats the best approach?

Upvotes: 2

Views: 174

Answers (1)

İsmet Alkan
İsmet Alkan

Reputation: 5447

I think opening a new table requires too much effort for such a simple task. Of course it's up to your development/design style, but I recommend adding a new column to your present table and hold dropdown list IDs within it.

Solution 1:

ID    Text                       Dropdowns
1     This is some text          -DD1-DD2-
2     Some more text             -DD2-DD3-
3     Even more text             -DD3-
4     This is just silly now     -DD4-

You can populate the dropdowns with a slight change in the code. The reason I used separators is to handle situations like looking for DD1 succeeds in the text DD10.

You will have to use -DropdownID- (or any separator you choose to use with the same format) for string containment search. Editing the value within that column, assuming - is the separator:

  • Assign - as default value of the column in the database side and columns will hold - if they're empty.
  • Adding a new dropdown ID, first look for your ID is contained, than concatenate DropdownID- to column value.
  • Deleting a dropdown ID, just replace DropdownID- with empty string: "".
  • As told before, use -DropdownID- for search.

If every entity can only be shown in one dropdown at once, you can just simply write dropdown's name to the new column and look for equality instead of containment.

Solution 2:

I see that you thought about opening new database tables for each dropdown list. It's a very bad approach. You can build something like:

     -- Texts--                     -- Dropdowns --          -- DropdownTexts --
ID    Text                       ID     ViewID   >..<    ID     TextID     DropdownID
1     This is some text          1       DD1             1       1            1
2     Some more text             2       DD2             2       1            3
3     Even more text             3       DD3             3       2            3
4     This is just silly now                             4       4            1

I opened the table Dropdowns in case you need database side help with your implementation, you can add more columns to Dropdowns to make it more configurable. If you don't, ignore Dropdowns table and directly write the DropdownID in your View to DropdownID in DropdownTexts table.

Upvotes: 1

Related Questions