[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Drop down list items appended on postback (need replaced)

Posted on 2009-05-13
9
Medium Priority
?
279 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

649 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