前端代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>個人愛好</h2>
<asp:CheckBox ID="checkBox1" runat="server" Text="唱歌" AutoPostBack="True" OnCheckedChanged="checkBox1_CheckedChanged" />
<asp:CheckBox ID="checkBox2" runat="server" Text="跳舞" AutoPostBack="True" OnCheckedChanged="checkBox2_CheckedChanged" />
<asp:CheckBox ID="checkBox3" runat="server" Text="打籃球" AutoPostBack="True" OnCheckedChanged="checkBox3_CheckedChanged" />
<asp:CheckBox ID="checkBox4" runat="server" Text="唱Rap" AutoPostBack="True" OnCheckedChanged="checkBox4_CheckedChanged" />
</div>
<div>
<h2>你最喜歡的有</h2>
<asp:CheckBoxList runat="server" ID="checkBoxList">
<asp:ListItem>C</asp:ListItem>
<asp:ListItem>Java</asp:ListItem>
<asp:ListItem>C#</asp:ListItem>
<asp:ListItem>Python</asp:ListItem>
<asp:ListItem>C++</asp:ListItem>
<asp:ListItem>PHP</asp:ListItem>
</asp:CheckBoxList>
<asp:Button runat="server" Text="提交" ID="btn" OnClick="btn_Click" />
</div>
</form>
</body>
</html>
數據庫連接類:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
namespace WebApplication4
{
public class DB
{
public static SqlConnection sqlcon()
{
//這種連接數據庫的方法要把登陸數據庫的方式改爲SQL Server身份驗證
//server="服務器名稱";database="要連接的數據庫名稱"
//uid="登錄名";pwd="密碼"
return new SqlConnection("server=.;database=CheckBoxInfo;uid=wda;pwd=dwda");
}
}
}
後端代碼:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
//static string loveStr = "您的愛好有:";
protected void Page_Load(object sender, EventArgs e)
{
//IsPostBack,表示返回時
if (!IsPostBack)
{
//只賦值一次
ViewState["love"] = "您的愛好有:";
dataBind();
}
//ViewState視圖狀態,內置對象,只在當前頁面生效,不能夠跨頁面,可以跨頁面的有,response request
}
protected void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//如果當前的複選框是選中狀態的時候
if (this.checkBox1.Checked)
{
ViewState["love"] = ViewState["love"].ToString() + " " + this.checkBox1.Text;
// loveStr += this.checkBox1.Text;
Response.Write(ViewState["love"].ToString());
}
else
{
string str = " " + this.checkBox1.Text;
//將原來的數據替換成空格
ViewState["love"] = ViewState["love"].ToString().Replace(str, "");
Response.Write(ViewState["love"].ToString());
}
}
protected void checkBox2_CheckedChanged(object sender, EventArgs e)
{
//如果當前的複選框是選中狀態的時候
if (this.checkBox2.Checked)
{
ViewState["love"] = ViewState["love"].ToString() + " " + this.checkBox2.Text;
//loveStr += this.checkBox2.Text;
Response.Write(ViewState["love"].ToString());
}
else
{
string str = " " + this.checkBox2.Text;
//將原來的數據替換成空格
ViewState["love"] = ViewState["love"].ToString().Replace(str, "");
Response.Write(ViewState["love"].ToString());
}
}
protected void checkBox3_CheckedChanged(object sender, EventArgs e)
{
//如果當前的複選框是選中狀態的時候
if (this.checkBox3.Checked)
{
ViewState["love"] = ViewState["love"].ToString() + " " + this.checkBox3.Text;
//loveStr += this.checkBox2.Text;
Response.Write(ViewState["love"].ToString());
}
else
{
string str = " " + this.checkBox3.Text;
//將原來的數據替換成空格
ViewState["love"] = ViewState["love"].ToString().Replace(str, "");
Response.Write(ViewState["love"].ToString());
}
}
protected void checkBox4_CheckedChanged(object sender, EventArgs e)
{
//如果當前的複選框是選中狀態的時候
if (this.checkBox4.Checked)
{
ViewState["love"] = ViewState["love"].ToString() + " " + this.checkBox4.Text;
//loveStr += this.checkBox2.Text;
Response.Write(ViewState["love"].ToString());
}
else
{
string str = " " + this.checkBox4.Text;
//將原來的數據替換成空格
ViewState["love"] = ViewState["love"].ToString().Replace(str, "");
Response.Write(ViewState["love"].ToString());
}
}
protected void btn_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.checkBoxList.Items.Count; i++)
{
if (checkBoxList.Items[i].Selected)
{
Response.Write(checkBoxList.Items[i].Text + " ");
}
}
}
public void dataBind()
{
SqlConnection sqlcon = DB.sqlcon();
sqlcon.Open();
SqlCommand sqlcom = new SqlCommand("select * from info", sqlcon);
SqlDataReader dr = sqlcom.ExecuteReader(); //只讀器,找出所有符合條件的數據集合
this.checkBoxList.DataSource = dr;
//對應數據表info中的字段id
this.checkBoxList.DataValueField = "id";
//對應數據表info中的字段name
this.checkBoxList.DataTextField = "name";
this.checkBoxList.DataBind(); //數據綁定
sqlcon.Close();
}
}
}