I have created a form where I add dropdowns based on form fields from a PDF.

The `namesMapping` variable is a `Dictionary` containing the old values as keys and the new ones as values. To save the values I want to add the current selection to the `Dictionary`. **But the eventlistener is not firing.** 

I have read many threads here that say use the `IsPostBack` variable to not update the dropdowns on page reload. But it doesn't seem to work in this case. 

It feels like it should be easier to get the selected values from dropdowns. But anyways, **how do I do to save the selected values from the dropdowns so I can get them after I click the button?**

Here is the code:

```
    protected void Page_Load(object sender, EventArgs e)
    {
        ListItem[] listItems = new ListItem[4];
        listItems[0] = new ListItem("First Name", "first-name");
        listItems[1] = new ListItem("Last Name", "last-name");
        listItems[2] = new ListItem("Email", "email");
        listItems[3] = new ListItem("Company", "company");

        int counter = 0;
        foreach (PdfField field in fields)
        {
            if (field.GetType() == typeof(PdfTextField))
            {
                TableRow tr = new TableRow();
                TableCell name = new TableCell
                {
                    Text = field.Name
                };
                tr.Cells.Add(name);
                TableCell options = new TableCell();

                DropDownList dropdown = new DropDownList();
                dropdown.ID = field.Name;
                for (int i = 0; i < listItems.Length; i++)
                {
                    dropdown.Items.Add(listItems[i]);
                }
                dropdown.SelectedIndexChanged += Dropdown_SelectedIndexChanged;

                options.Controls.Add(dropdown);
                tr.Cells.Add(options);
                Table1.Rows.Add(tr);

                //If page loads for the first time, set the mapping to the default selected values
                if (!IsPostBack)
                {
                    namesMapping.Add(field.Name, dropdown.SelectedValue);
                }
            }
        }

    private void Dropdown_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dropDown = (DropDownList) sender;
        namesMapping[dropDown.ID] = dropDown.SelectedValue;
    }

```

Thanks for answers.

**Edit**

I moved the code where the dropdowns are created to be within `if(!IsPostBack)` so they are not recreated when reloading the page. I save all the dropdowns in a `Dictionary`, but they still show the first value as the selected when selecting another in the dropdown.

Here is the code: 

```
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            doc = new PdfDocument(pdfFileName);

            namesMapping = new Dictionary<string, string>();
            ddlMappings = new Dictionary<string, DropDownList>();

            //Get all fields
            fields = doc.Fields;

            //Create the dropdown lists
            dropdownLists = new List<DropDownList>();

            ListItem[] listItems = new ListItem[4];
            listItems[0] = new ListItem("First Name", "first-name");
            listItems[1] = new ListItem("Last Name", "last-name");
            listItems[2] = new ListItem("Email", "email");
            listItems[3] = new ListItem("Company", "company");

            int counter = 0;
            foreach (PdfField field in fields)
            {
                //Only display the text fields
                if (field.GetType() == typeof(PdfTextField))
                {
                    TableRow tr = new TableRow();
                    //Add the field name
                    TableCell name = new TableCell
                    {
                        Text = field.Name
                    };
                    tr.Cells.Add(name);
                    //Add the options to select from
                    TableCell options = new TableCell();

                    DropDownList dropdown = new DropDownList();
                    dropdown.ID = field.Name;

                    //Populate the dropdown
                    for (int i = 0; i < listItems.Length; i++)
                    {
                        dropdown.Items.Add(listItems[i]);
                    }

                    dropdown.AutoPostBack = true;
                    dropdown.SelectedIndexChanged += Dropdown_SelectedIndexChanged;

                    options.Controls.Add(dropdown);
                    tr.Cells.Add(options);
                    //Add row to table
                    Table1.Rows.Add(tr);

                    //If page loads for the first time, set the mapping to the default selected values
                    namesMapping.Add(field.Name, dropdown.SelectedValue);
                    ddlMappings.Add(field.Name, dropdown);
                }
            }

        }
        else
        {
            foreach (PdfField field in fields)
            {
                //Only display the text fields
                if (field.GetType() == typeof(PdfTextField))
                {
                    TableRow tr = new TableRow();
                    //Add the field name
                    TableCell name = new TableCell
                    {
                        Text = field.Name
                    };
                    tr.Cells.Add(name);
                    //Add the options to select from
                    TableCell options = new TableCell();

                    DropDownList dropdown = ddlMappings[field.Name];
                    dropdown.SelectedIndexChanged += Dropdown_SelectedIndexChanged;

                    options.Controls.Add(dropdown);
                    tr.Cells.Add(options);
                    //Add row to table
                    Table1.Rows.Add(tr);

                }
            }

        }

        if (!IsPostBack)
        {
            foreach(var ddl in ddlMappings)
            {
                ddl.Value.SelectedValue = namesMapping[ddl.Key];
            }
        }
    }

    private void Dropdown_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList dropDown = (DropDownList) sender;
        namesMapping[dropDown.ID] = dropDown.SelectedValue;
    }

```