2

我正在使用向导。在第二步中,用户可以添加有关将要存储的位置的信息。他可以根据需要添加任意数量的位置。在第三步中,我想输入有关特定位置的更多信息。要选择要使用的位置,我有 ASP:dropdownlist,当用户输入内容时我会填充它。我想更改索引,但它不起作用当我尝试调试时它永远不会进入函数。我正在使用标签进行调试。

当我更改所选项目时,页面会重新加载,并且下拉列表中的第一个项目始终被选中,即使我选择了其他内容。我不明白为什么会这样

这是我拥有的 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";
    }
4

1 回答 1

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();
    }
}
于 2013-04-14T00:36:41.797 回答