asp.net 使用UpdatePanel实现局部刷新

实现功能: 第一个DropDownList改变时,更新另外一个DropDownList和Label;第二个DropDownList改变时更新Label

前台设计:2个DropDownList ,2个UpdatePanel,1个Label,其中1个UpdatePanel包含1个DropDownList

                                                                                                                                                                                                                                                                                                       




注意:ScriptManager控件必须在所有UpdatePanel控件之前,DropDownList改变时必须设置回发(AutoPostBack="True")


最重要的一点就是在使用UpdatePanel时,不能使用Response.Write(),否则将不能刷新,这是很致命的一点


后台代码:

      

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    Label1.Text = DropDownList1.SelectedItem.Text;

    if (DropDownList1.SelectedItem.Text == "我")
    {
        DropDownList2.Items.Clear();
        DropDownList2.Items.Add("我1");
        DropDownList2.Items.Add("我2");
        DropDownList2.Items.Add("我3");
    }
    if (DropDownList1.SelectedItem.Text == "你")
    {
        DropDownList2.Items.Clear();
        DropDownList2.Items.Add("你1");
        DropDownList2.Items.Add("你2");
        DropDownList2.Items.Add("你3");
    }
    if (DropDownList1.SelectedItem.Text == "他")
    {
        DropDownList2.Items.Clear();
        DropDownList2.Items.Add("他1");
        DropDownList2.Items.Add("他2");
        DropDownList2.Items.Add("他3");
    }
}

protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
    Label1.Text = DropDownList2.SelectedItem.Text;
}


你可能感兴趣的:(asp.net 使用UpdatePanel实现局部刷新)