0
votes

I am having an issue with a dropdownlist inside of a gridview.

The gridview is update on the rowcreated command with a dropdownlist and a set of values (which works fine). The selected values in the dropdownlist can be changed by the user and then saved.

When I click save, I need to get each new selected value from the dropdownlist and store it in an int list if it hasnt already been added.

This all works OK but I can't get the updated value that the user selected, only what was already populated as the selected item when it was setup.

protected void btnSave_Click(object sender, EventArgs e)
{
    List<int> list = new List<int>();
    foreach (GridViewRow r in gvOrder.Rows)
    {
        if (r.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = r.FindControl("ddlNewO") as DropDownList;
            int value = Convert.ToInt16(ddl.SelectedItem.Text);
            bool Contains = list.Contains(value);
            if (!Contains)
            {
                list.Add(value);
            }
            else
            {
                lblMessage.Text = "This value is already assigned!";
            }
        }
    }
}

Example: if there are 9 rows, each dropdown has numbers 1-9 and it's selected number is the row index. If I go in after page load and select the first three as '1' and click save, it runs through and still picks out the values from when it was loaded, e.g. 1,2,3.

1

1 Answers

0
votes

I needed to ensure that my gridview didnt bind twice:

        if (!IsPostBack)
        {
            PopulateGridview();
        }