ASP:Dropdownlist onselectedindexchanges function does not fire even when I set autopostback to true

StackOverflow https://stackoverflow.com/questions/15994429

Question

I am using a wizard. and in step two the user can add information about a location that will be stored. He can add as many locations as he likes. In step three I want to enter more information about the specific location. To select which location to use I have ASP:dropdownlist that I populate when user enters stuff. I want to change index but it just does not work It never goes into the function when I tried debugging. I am using a label to debug.

When I change the selected item the page reloads and the first item in the dropdown list is always selected even though I selected something else. I do not understand why that happens

here is what I have ASPX File

Select location to enter data for:
<asp:DropDownList ID="s3_location_list" runat="server" AutoPostBack="true" OnSelectedIndexChanged="stepThree_location_list_SelectedIndexChanged">
                </asp:DropDownList>
                &nbsp;&nbsp; Current location index:
                <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>

CS files

/*This is where I add data to the drop down list

protected void stepTwo_addLocationData(object Sender, System.EventArgs e)
    {
        //initialize a temporary string array to get all the data from the form

        //Console.Write("AAAAA"+ Context.Items["IsRefreshed"]);

        Boolean refreshed = (Boolean)Context.Items["IsRefreshed"];
        if (!refreshed)
        {
            //if user is adding a new entry to the location table

            LocationData new_location = new LocationData();
            //add stuff to locationData

            if (stepTwo_locationTableIndex == -1)
            {
                //add the new_location element into the location_data array
                location_data.Add(new_location);

                //redraw the table
                DataRow dr = dt.NewRow();
                dr["Location"] = new_location.City;
                //dr["Name"] = loc;
                dt.Rows.Add(dr);


            }
            else
            {
                location_data[stepTwo_locationTableIndex] = new_location;
                dt.Rows[stepTwo_locationTableIndex]["Location"] = City.Text;
            }

            GridView1.DataSource = dt.DefaultView;
            GridView1.DataBind();
            ///CreateTable();
            stepFive_setLocationListOptions();
            stepThree_setLocationListOptions();
            stepTwo_resetForm();
     }

/*this is the page load on step 3

protected void stepThree_load()
    {
        if (!IsPostBack && Session["s_s3_locationDropdownIndex"] == null)
        {
            stepThree_locationDropdownIndex = s3_location_list.SelectedIndex;
            Session["s_s3_locationDropdownIndex"] = stepThree_locationDropdownIndex;
        }
        else
        {
            stepThree_locationDropdownIndex = (int) Session["s_s3_locationDropdownIndex"];
        }

        s3_location_list.SelectedIndex = stepThree_locationDropdownIndex;
        Label3.Text = "" + s3_location_list.SelectedIndex;

    }

/*this is my where I populate the list

    protected void stepThree_setLocationListOptions()
    {
        s3_location_list.Items.Clear();
        int i = 0;
        foreach (LocationData item in location_data)
        {
            s3_location_list.Items.Add(new ListItem(item.City, "" + i));
        }
        s3_location_list.DataBind();
    }

/* this is the function thats callled when selected index is changed.

    protected void stepThree_location_list_SelectedIndexChanged(object sender, EventArgs e)
    {
        Label3.Text = "Hello";
    }
Was it helpful?

Solution

I think the problem is the order of execution. You should only initialize a DropDownList once, otherwise it will overwrite your SelectedIndex. For example, use the OnInit event:

<asp:DropDownList ID="s3_location_list"
                  runat="server"
                  OnInit="s3_location_list_Init"/>

And write the event method like this:

protected void s3_location_list_Init(object sender, EventArgs e)
    if (!IsPostBack) {
        foreach (LocationData item in location_data)
        {
            s3_location_list.Items.Add(new ListItem(item.City, "" + i));
        }
        s3_location_list.DataBind();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top