Click here to Skip to main content
16,016,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.....

C#
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
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.

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.
 
Share this answer
 
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. :)
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

C#
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();
    }
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900