バージョン:ASP.NET 3.5
AutoPostBackとは、DropDownListやCheckBox、RadioButtonなどのコントロールに、
「AutoPostBack=true」が設定されている場合、項目の選んだ場合や、チェックをつけた場合、外した場合などに
サーバへ処理を送信したい場合に使う。
下記は、DropDownListを「AutoPostBack=true」に設定し、
どの項目を選んだかをLabel1に表示している。
●Default.aspx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | < html > < head runat = "server" > < title ></ title > </ head > < body > < form id = "form1″ runat=" server"> < div > < asp:DropDownList ID = "DropDownList1″runat=" server" AutoPostBack = "true" OnSelectedIndexChanged = "on_changed" > < asp:ListItem >aaa</ asp:ListItem > < asp:ListItem >bbb</ asp:ListItem > < asp:ListItem >ccc</ asp:ListItem > </ asp:DropDownList > < br /> < asp:Label ID = "Label1" runat = "server" ></ asp:Label > </ div > </ form > </ body > </ html > |
●Default.aspx.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default6 : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { } protected void on_changed( object sender, EventArgs e) { Label1.Text = ((DropDownList)sender).SelectedItem.Text + "が選択されました。" ; } } |