Solved

Drop down list items appended on postback (need replaced)

Posted on 2009-05-13
9
266 Views
Last Modified: 2012-08-13
I have 2 drop down boxes. When a selection is made from the first one then the second is populated based on the selection. If the selection is changed in the first drop down then the second list has the items for the new selection but also retains the items from previous selections.
I want all the items in DDL2 to be wiped and replaced with the new items not appended.
Did that make sense?
I have tried using
Protected Sub DDGroups_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDGroups.SelectedIndexChanged
        DDCats.ClearSelection()
        DDCats.DataBind()
    End Sub

But no good
0
Comment
Question by:QPR
  • 5
  • 3
9 Comments
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 150 total points
ID: 24380409
try using DDCats.Items.Clear() - although I would have thought the databind would already clear the items.
0
 
LVL 6

Expert Comment

by:M3mph15
ID: 24380963
Hi,

Firstly make sure that AppendDataBoundItems is set to false for the dropdownlist.

Also if the automatic methods do work, you can do manually:

For i = 0 To DDCats.Items.Count - 1
            DDCats.Items.RemoveAt(0)
Next

This will remove all items.

HTH
-M3mph15
0
 
LVL 29

Author Comment

by:QPR
ID: 24381061
"AppendDataBoundItems is set to false "

this is set to true as I need to add a default value.
<asp:ListItem Value="0" Text="Category"></asp:ListItem>
I use this because I don't want them to not select a category which would have the affect of selecting the top one by default. My required field validator has an initial value of 0
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 29

Author Comment

by:QPR
ID: 24381070
DDCats.Items.Clear()
works perfectly - now if I could get it to re-append my default item...
0
 
LVL 6

Expert Comment

by:M3mph15
ID: 24381328
Before you clear DDCats save the default item.

I'm going to assume its your first item. So something like ListItem liDefault = DDCats.Items(0);
Then you can add the item back to DDCats after you clear it

-M3mph15
0
 
LVL 29

Author Comment

by:QPR
ID: 24381609
How do i save the default item?
What I need is a code behind version of this:
<asp:ListItem Value="0" Text="Category"></asp:ListItem>

I have tried
DDCats.Items.Clear()
        DDCats.Items.Add("Category")
        DDCats.DataBind()

which puts "category" at the top but it has no value and so slips past the validation
0
 
LVL 6

Accepted Solution

by:
M3mph15 earned 350 total points
ID: 24382240
Ok 2 ways.

1 is by saving the value that is already there:

ListItem li = new ListItem()
li = DDCats.Items(<index>)  //index is the index of the default item...first item is index 0.
DDCats.Clear()
DDCats.Items.Add(li)
DDCats.DataBind()

2 is if the value of the default value is always the same then you can hardcode it...but i wouldn't recommend incase you want to change it:

ListItem li = new ListItem()
li.Text = "<text that is shown in the dropdownlist>"
li.Value = "<value>"
DDCats.Clear()
DDCats.Items.Add(li)
DDCats.DataBind()

HTH
-M3mph15

0
 
LVL 29

Author Comment

by:QPR
ID: 24382730
looks good thanks I'll try it when at work tomorrow.
"Select a category" has the value 0
This will always be zero as the categories from the DB have a CatID and that CatID is an identity field which can never be zero. By using a required field validator and saying the initial value = 0 I can always be sure that a user has not overlooked the drop down list and is required to select a proper category from it before submitting.
0
 
LVL 29

Author Comment

by:QPR
ID: 24391411
Had to alter the first line as am using vb.net but this (below) works perfectly thanks!
        Dim li = New ListItem()
        li.Text = "Category"
        li.Value = "0"
        DDCats.Items.Clear()
        DDCats.Items.Add(li)
        DDCats.DataBind()


0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question