DropDownList 控件控制Image控件动态显示图像
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon;
string strCon = "Data Source=(local);Database=db_04;Uid=sa;Pwd=";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sqlcon = new SqlConnection(strCon);
sqlcon.Open();
string sqlstr = "select * from tb_Image";
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
myda.Fill(myds, "tb_Image");
ddlPhoto.DataSource = myds.Tables["tb_Image"];
ddlPhoto.DataTextField = "Image";
ddlPhoto.DataBind();
imgPhoto.ImageUrl = ddlPhoto.SelectedValue;
sqlcon.Close();
}
}
protected void ddlPhoto_SelectedIndexChanged(object sender, EventArgs e)
{
imgPhoto.ImageUrl = ddlPhoto.SelectedValue;
}
}
SelectedIndexChanged 事件 当列表控件中的选择在向服务器的发送过程中更改时会引发这个事件 记得 DropDownList的AutoPostBack属性要为True