ASP:Dropdownlist onselectedindexchanges函数即使我将autopostback设置为true也不会触发

时间:2013-04-14 00:29:50

标签: asp.net drop-down-menu autopostback

我正在使用向导。在第二步中,用户可以添加有关将要存储的位置的信息。他可以根据自己的喜好添加多个位置。 在第三步中,我想输入有关特定位置的更多信息。 要选择使用哪个位置,我有ASP:用户输入内容时填充的下拉列表。 我想改变索引,但它只是不起作用它在我尝试调试时从未进入函数。我正在使用标签进行调试。

当我更改所选项目时,页面会重新加载,即使我选择了其他内容,也会始终选择下拉列表中的第一项。我不明白为什么会发生这种情况

这就是我所拥有的 ASPX文件

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文件

/ *这是我在下拉列表中添加数据的地方

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();
     }

/ *这是第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;

    }

/ *这是我填充列表的地方

    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();
    }

/ *这是在选择索引更改时调用的函数。

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

1 个答案:

答案 0 :(得分:1)

我认为问题在于执行的顺序。您应该只初始化一次DropDownList,否则会覆盖您的SelectedIndex。例如,使用OnInit事件:

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

并编写如下事件方法:

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();
    }
}