private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面

DropDownList1.Items.Clear();
DropDownList1.Items.Add ("VB");
DropDownList1.Items.Add ("C#");
DropDownList1.Items.Add ("VB.NET");
DropDownList1.Items.Add ("Asp.NET");
}private void Button1_Click(object sender, System.EventArgs e)
{
  Label1.Text ="您已经选择了" + DropDownList1.SelectedItem.Text;
}
为什么每次点完按钮    都显示“您已经选择了 VB” 谁能讲讲原理。
我没学过ASP.NET ASP C# 但我会VB VB.NET 大家有忠告给我这样得新手吗?
谢谢

解决方案 »

  1.   

    if(!ispostback)
    {
    DropDownList1.Items.Clear();
    DropDownList1.Items.Add ("VB");
    DropDownList1.Items.Add ("C#");
    DropDownList1.Items.Add ("VB.NET");
    DropDownList1.Items.Add ("Asp.NET");
    }
      

  2.   

    ispostback是来判断是否是第一次加载页面,true--第一次,false--起码第二次
    你的页面每次加载时page_load是必执行的,尽管你用了Button1_Click,但每次你按下按钮后事实是先执行page_load再执行Button1_Click。
    所以你的程序就是执行
            DropDownList1.Items.Clear();
    DropDownList1.Items.Add ("VB");
    DropDownList1.Items.Add ("C#");
    DropDownList1.Items.Add ("VB.NET");
    DropDownList1.Items.Add ("Asp.NET");
    这段。而vb选项是默认selected的,所以在Button1_Click中只会显示vb选项,而不是你选的。if(!ispostback) //第一次加载时运行
    {
    DropDownList1.Items.Clear();
    DropDownList1.Items.Add ("VB");
    DropDownList1.Items.Add ("C#");
    DropDownList1.Items.Add ("VB.NET");
    DropDownList1.Items.Add ("Asp.NET");
    }
      

  3.   

    页面每次加载时page_load是必执行的,里面的代码是向DropDownList1加入项,比如你将
    DropDownList1.Items.Clear();句注释你就会发现其中的原理了
      

  4.   

    if(!ispostback) 出错???
    少引用??