ASP.NET--SQL中内容显示到CheckBoxList中

创建窗体

1.创建连接数据库的类DB.cs。
2.创建接收并显示数据库中值的Web窗体。
3.在Web窗体上建立CheckBoxList,用来显示SQL的值供选择
4.在Web窗体上建立Button按钮,目的是为了在运行时选择好SQL中的值之后点击好观察是否成功选择。

代码展示

数据库连接(DB.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;              //连接数据库必须建立项
using System.Data.SqlClient;    //连接数据库必须建立项

namespace 登陆数据库
{
    public class DB
    {
        public static SqlConnection createConnection()     //创建连接的方法
        {
            System.Data.SqlClient.SqlConnection con = 
            new SqlConnection("server=.;database=login;uid=sa;pwd=123;");  //创建连接
            return con;                                    //返回链接
        }
    }
}

显示界面:
这里写图片描述
界面属性设计代码(CheckBoxList.aspx):

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Like.aspx.cs" Inherits="登陆数据库.Like" %>

<!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>
        </div>
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" Height="185px" RepeatColumns="3" Width="458px" align="center" BorderWidth="1" BorderColor="red">    
        </asp:CheckBoxList>
        </asp:RadioButtonList>
        <div align="center"><!--在Button控件中没有找到归中属性,所以设立此<div>-->
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="height: 21px" Text="显示" />
        </div>
    </form>
</body>
</html>

界面后台代码(CheckBoxList.aspx.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace 登陆数据库
{
    public partial class Like : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)                               //判断页面是否第一次加载
            {
                SqlConnection con = DB.createConnection();      //连接数据库连接类
                con.Open();                                     //打开数据库连接
                SqlCommand cmd = new SqlCommand("select * from personLike", con);
                SqlDataReader sdr = cmd.ExecuteReader();        //用sdr来接收数据库中查询到的信息
                this.CheckBoxList1.DataTextField = "likeContent"; //用CheckBoxList的DataTextField属性来接收数据库中likeContent内容
                this.CheckBoxList1.DataValueField = "id";       //用CheckBoxList的DataValueField属性来接收数据库中id内容
                this.CheckBoxList1.DataSource = sdr;            //用CheckBoxList的DataSource属性来接收sdr内容
                this.CheckBoxList1.DataBind();                  //绑定到CheckBoxList
                sdr.Close();
                con.Close();
            }             
        }
        protected void Button1_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < this.CheckBoxList1.Items.Count ; i++)   //循环.每点击一次按钮循环一次
            {
                if (this.CheckBoxList1.Items[i].Selected)               //判断.CheckBoxList中的值是否被选中
                {
                    Response.Write(this.CheckBoxList1.Items[i].Value.ToString() + "--" + this.CheckBoxList1.Items[i].Text + "</br>");                            //以“序号--名称”显示到页面
                }
            }
        }
    }
}

总结

从上文可以看出Web服务器控件下选择控件中的CheckBoxList控件有这么些作用,那么同样是选择控件的RadioButtonList控件、ListBox控件、DropDownList控件有什么内容呢?用处又是什么呢?

end

谢谢您的细心阅读,如有不当之处,敬请您指出,我将改进!

猜你喜欢

转载自blog.csdn.net/luojun13class/article/details/80799720