|
外觀Body內: <form id="form1" runat="server"> <div> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="填充DropDownList" /> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="選擇一項" /> <asp:Label ID="Label1" runat="server"></asp:Label><br /> <br /> AutoPostBack設置爲true後,觸發OnSelectedIndexChanged的事件立刻發送到服務器</div> </form> 內部事件: protected void Button1_Click(object sender, EventArgs e) { DropDownList1.Items.Add(new ListItem("name","spell")); DropDownList1.Items.Add(new ListItem("age", "24")); DropDownList1.Items.Add(new ListItem("sex", "boy")); } protected void Button2_Click(object sender, EventArgs e) { Label1.Text =DropDownList1.SelectedItem.Text+" : " +DropDownList1.SelectedItem.Value; } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Label1.Text = DropDownList1.SelectedItem.Text + " : " + DropDownList1.SelectedItem.Value; } |