Solved

Drop down list items appended on postback (need replaced)

Posted on 2009-05-13
9
270 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 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
Technology Partners: 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 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

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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.…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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