Click here to Skip to main content
12,509,899 members (57,262 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hello

I'm trying to construct a dropdown list in a code behind page, based on the values of a FormView.

I have most of the logic sorted (I think) to put in the correct values, but when I select an item from the drop down list, the orginial drop down list is appended with the same values again, so the list keeps growing and growing, when all I want it to do is retain the selected item and the original list content (no matter how many times the page is posted back from the drop down).

I have placed the following code in the Page_Load event section, could this be causing the problem?

Any help appreciated, but beaware, I'm a newbie in this.....

DropDownList3.Items.Add("-");
if (callDetails.DataKey["SOURCE_REFERENCE"] != "")
{
    DropDownList3.Items.Add("Source");
}
if (callDetails.DataKey["SUBJECT_REFERENCE"] != "")
{
    DropDownList3.Items.Add("Subject");
}
if (callDetails.DataKey["REFERENCE"] != "")
{
    DropDownList3.Items.Add("3rd Party");
}
Posted 18-Oct-10 23:21pm
Comments
Hiren Solanki 19-Oct-10 6:00am
   
after solution provided by brij I can add or I am assuming that you may have Autopostback = 'true' with DropDownList.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Put your code like

if(!IsPostBack)
{
DropDownList3.Items.Add("-");
if (callDetails.DataKey["SOURCE_REFERENCE"] != "")
{   
 DropDownList3.Items.Add("Source");
}
if (callDetails.DataKey["SUBJECT_REFERENCE"] != "")
{   
 DropDownList3.Items.Add("Subject");
}
if (callDetails.DataKey["REFERENCE"] != "")
{   
 DropDownList3.Items.Add("3rd Party");}
}
}

Now your code will not be executed more than once.
  Permalink  
Comments
codemagpie 19-Oct-10 5:48am
   
Hi Brij

I have tried that and yes it doesn't append to the list anymore, but the selected item value is not being passed now, it is just keep the first selected item value?

Any further help would be most appreciated.

Cheers

Pete
Hiren Solanki 19-Oct-10 5:59am
   
!IsPostBack annoys newbies much. :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi Brij

I used a combination of your code and something to handle if it is a postback.

Seems to do the job but there might be a slicker way.

Thanks anyway.

Cheers

Peter

if (!IsPostBack)
{
    DropDownList3.Items.Add(new ListItem("-", "-"));
    if (callDetails.DataKey["SOURCE_REFERENCE"] != "")
    {
        DropDownList3.Items.Add(new ListItem("Source", callDetails.DataKey["SOURCE_REFERENCE"].ToString()));
        Label10.Text = DropDownList3.SelectedValue.ToString();
    }
    if (callDetails.DataKey["SUBJECT_REFERENCE"] != "")
    {
        DropDownList3.Items.Add(new ListItem("Subject", callDetails.DataKey["SUBJECT_REFERENCE"].ToString()));
        Label10.Text = DropDownList3.SelectedValue.ToString();
    }
    if (callDetails.DataKey["REFERENCE"] != "")
    {
        DropDownList3.Items.Add(new ListItem("3rd Party", callDetails.DataKey["REFERENCE"].ToString()));
        Label10.Text = DropDownList3.SelectedValue.ToString();
    }
}
 
if (IsPostBack)
{
    if (callDetails.DataKey["SOURCE_REFERENCE"] != "")
    {
        Label10.Text = DropDownList3.SelectedValue.ToString();
    }
    if (callDetails.DataKey["SUBJECT_REFERENCE"] != "")
    {
        Label10.Text = DropDownList3.SelectedValue.ToString();
    }
    if (callDetails.DataKey["REFERENCE"] != "")
    {
        Label10.Text = DropDownList3.SelectedValue.ToString();
    }
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160929.1 | Last Updated 19 Oct 2010
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100