ASP.NET选中复选框在页面上提示选中信息以及复选框文字采用数据库生成

前端代码:

<%@ 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(); 
        }
    }
}

猜你喜欢

转载自blog.csdn.net/jianjianshini/article/details/106169997