您的位置:首页 > 其它

使用列表控件展示数据

2009-12-07 21:40 417 查看
大概界面如图;





前台代码:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDoenList_SelectedIndexChanged">














后台代码:

protected void Button5_Click(object sender, EventArgs e)
    {
        var cn = new SqlConnection();
        cn.ConnectionString = @"Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|Student.Mdf;Integrated Security=True;User Instance=True";
        var cmd = new SqlCommand();
        cmd.Connection = cn;
        cmd.CommandText = "select * from student ";

        cn.Open();

        var dr = cmd.ExecuteReader();
        DropDownList1.DataSource = dr;
        DropDownList1.DataTextField = "StudentName";
        DropDownList1.DataValueField = "StudentID";
        DropDownList1.DataBind();
        dr.Close();
        cn.Close();
    }
    protected void DropDoenList_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write("Value:"+DropDownList1.SelectedValue+"</br>");
        Response.Write(  DropDownList1.SelectedItem.Text+"</br>");
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐