checkboxlist的用法 :直接根据数据库中表的字段来设置多选菜单。
//创建连接数据库的静态类
//静态类不需要新建对象
string sql = "server=.;database=list;integrated Security=SSPI;";
SqlConnection con = new SqlConnection(sql);
return con;
//绑定
string sql = "select * from personLike";
SqlConnection con = DB.createConnection();
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader dr = cmd.ExecuteReader();
this.CheckBoxList1.DataSource = dr;
this.CheckBoxList1.DataTextField = "likeContent";
this.CheckBoxList1.DataValueField = "id";
this.CheckBoxList1.DataBind();
dr.Close();
con.Close();
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.CheckBoxList1.Items.Count - 1; i++)
{
if(this.CheckBoxList1.Items[i].Selected)
{
//如果是获取选中项的主键值用value,获取文本值用text
Response.Write(this.CheckBoxList1.Items[i].Value.ToString()+"-"+this.CheckBoxList1.Items[i].Text+"<br>");
}
}
radiobuttonlist的用法和checkboxlist的用法基本相同,前者是单选,后者可以多选。
Imagebutton用法:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
//X和Y可以控制Imagebutton的坐标、
if (e.X < 150)
{
Response.Write("Left");
}
else
{
Response.Write("right");
}