2015年6月14日 星期日

asp.net dropdownlist selectedindexchanged

真是奇妙的運用 , 好不習慣!! 習慣了 windows app 開發 , web 有點...
參考引用來源:DropDownList的SelectedIndexChanged事件
底下code :
---

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. SqlConnection conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DBconnStr"].ToString());
  6. SqlDataAdapter sqlda = new SqlDataAdapter();
  7. sqlda.SelectCommand = new SqlCommand("select id,name from demouser", conn);
  8. DataTable dt = new DataTable();
  9. sqlda.Fill(dt);
  10. this.DropDownList1.DataSource = dt;
  11. this.DropDownList1.DataTextField = "name";
  12. this.DropDownList1.DataValueField = "id";
  13. this.DropDownList1.DataBind();
  14. }
  15. }
  16. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  17. {
  18. this.TextBox2.Text = this.DropDownList1.SelectedItem.Value;
  19. }
  1. <form id="form1" runat="server">
  2. <br />
  3. <br />
  4. <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
  5. </asp:DropDownList>
  6. <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  7. </form>

沒有留言:

張貼留言