?
Solved

Drop down list items appended on postback (need replaced)

Posted on 2009-05-13
9
Medium Priority
?
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 600 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 1400 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

770 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