2015年6月14日 星期日

asp.net dropdownlist selectedindexchanged

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

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DBconnStr"].ToString());
            SqlDataAdapter sqlda = new SqlDataAdapter();
            sqlda.SelectCommand = new SqlCommand("select id,name from demouser", conn);
            DataTable dt = new DataTable();
            sqlda.Fill(dt);
            this.DropDownList1.DataSource = dt;
            this.DropDownList1.DataTextField = "name";
            this.DropDownList1.DataValueField = "id";
            this.DropDownList1.DataBind();        
        }

    }
  
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.TextBox2.Text = this.DropDownList1.SelectedItem.Value;
    }
 <form id="form1" runat="server">
        <br />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList> 
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    </form>

沒有留言:

張貼留言