CheckBox用法

 

create table Book
(
	bookId int identity(1001,1),
	bookName varchar(50),
	bookAuthor varchar(50),
)
insert into Book values('Java程序设计','a')
insert into Book values('C#程序设计','b')
insert into Book values('Asp.net程序设计','v')
insert into Book values('Javascript程序设计','d')
insert into Book values('XML程序设计','e')


 

 

 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function CheckAll() {
            var ckBox = document.getElementById('book_table').getElementsByTagName('input');
            var ckAll = document.getElementById('ckBox_All');
            for (var i = 0; i < ckBox.length; i++) {
                if (ckBox[i].type == 'checkbox') {
                    ckBox[i].checked = ckAll.checked;
                }
            }
        }
        function DelCheckedItem() {
            var delBox = document.getElementsByTagName('input');
            var checkIds = new Array(delBox.length);
            var j = 0;
            for (var i = 0; i < delBox.length; i++) {
                if (delBox[i].checked == true && delBox[i].id != 'ckBox_All') {
                    checkIds[j] = delBox[i].id;
                    j++;
                }
            }
            if (j == 0) {
                alert('请选择需要删除的项');
                return;
            }
            alert(checkIds.slice(0, j));
            //  NoRefresh._Default.DelAllBooks(checkIds.slice(0, j));
            BindData();
        }
        function BindData() {
          
            var list = NoRefresh._Default.GetAll().value;
            document.getElementById('div1').innerHTML = "";
            var bindHTML = "<table id='book_table'><tr><td>编号</td><td>名称</td><td>作者</td><td>";
            bindHTML += "<input type='checkbox' id='ckBox_All' οnclick='CheckAll();'/>全选</td></tr>";
            if (list != null && list.length > 0) {
                for (var i = 0; i < list.length; i++) {
                    bindHTML += "<tr><td>" + list[i].BookId + "</td>";
                    bindHTML += "<td>" + list[i].Bookname + "</td>"
                    bindHTML += "<td>" + list[i].Bookauthor + "</td>";
                    bindHTML += "<td><input type='checkbox' id=" + list[i].BookId + "></input></td></tr>";

                }
                document.getElementById('div1').innerHTML = bindHTML;
            }
        }
        function Add() {
            var name = document.getElementById('txt_name').value;
            var author = document.getElementById('txt_author').value;
            if (name.length == 0 || author.length == 0) {
                alert('书名或作者不能为空');
                return;
            }
            NoRefresh._Default.AddBooks(name, author);
            BindData();
        }
    </script>
</head>
<body οnlοad="BindData();">
    <form id="form1" runat="server">
    <div id='div1'>
    
    </div>
    <input type="button" value="删除" οnclick=" DelCheckedItem();"/>
    <br />
    书名<input type="text" id="txt_name"/><br />
    作者<input type="text" id="txt_author"/>
    <input type="button" id="btn_add" value="添加" οnclick="Add();" />
    </form>
</body>
</html>


 

   public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
            }
        }
        [AjaxPro.AjaxMethod]
        public List<Books> GetAll()
        {
            return DBHelper.CreateConnection().GetAllBooks();
        }
        [AjaxPro.AjaxMethod]
        public bool DelAllBooks(int[] ids)
        {
            string[] sqls = new string[ids.Length];
            int i = 0;
            foreach (int id in ids)
            {
                sqls[i] = string.Format("delete from book where bookid={0}",id);
                i++;
            }
            return DBHelper.CreateConnection().DeleteBooksById(sqls);
        }
        [AjaxPro.AjaxMethod]
        public bool DeleteById(int id)
        {
            return DBHelper.CreateConnection().DeleteById(id); ;
        }
        [AjaxPro.AjaxMethod]
        public bool AddBooks(string name, string author)
        {
            Books book = new Books();
            book.Bookname = name;
            book.Bookauthor = author;
            return DBHelper.CreateConnection().AddNewBook(book);
        }





    }

public class DBHelper
    {
        static SqlConnection conn =null;
        static DBHelper db = null;
        private DBHelper()
        {
            conn = new SqlConnection("server=.;database=Example;uid=sa;pwd=**");

        }
        public static DBHelper CreateConnection()
        {
            if (db == null)
            {
                db = new DBHelper();
            }
            return db;
        }
        public List<Books> GetAllBooks()
        {
            try
            {
                string sql = "select bookid,bookname,bookauthor from book";
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                List<Books> list = new List<Books>();
               while(dr.Read())
                {
                    Books book = new Books();
                    book.BookId = Convert.ToInt32(dr["bookid"]);
                    book.Bookname = dr["bookname"].ToString();
                    book.Bookauthor = dr["bookauthor"].ToString();
                    list.Add(book);

                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                conn.Close();
            }
        }
        public bool DeleteById(int id)
        {
            string sql = string.Format("delete from book where bookid={0}",id);
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                return cmd.ExecuteNonQuery() > 0;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                conn.Close();
            }
        }
        public bool DeleteBooksById(string[] sqls)
        {
            bool flag = true;
            try
            {
                conn.Open();
                SqlTransaction tran = conn.BeginTransaction();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.Transaction = tran;
                for (int i = 0; i < sqls.Length; i++)
                {
                    cmd.CommandText = sqls[i];
                    if (cmd.ExecuteNonQuery() == 0)
                    {
                        flag = false;
                        tran.Rollback();
                        return flag;
                    }
                }
                flag = true;
                tran.Commit();
                return flag;

            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                conn.Close();
            }
        }
        public bool AddNewBook(Books book)
        {
            string sql = string.Format("insert into book values('{0}','{1}')",book.Bookname,book.Bookauthor);
            conn.Open();
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                return cmd.ExecuteNonQuery() > 0;
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                conn.Close();
            }
        }
    }

  public class Books
    {
        private int bookId;

        public int BookId
        {
            get { return bookId; }
            set { bookId = value; }
        }
        private string bookname;

        public string Bookname
        {
            get { return bookname; }
            set { bookname = value; }
        }
        private string bookauthor;

        public string Bookauthor
        {
            get { return bookauthor; }
            set { bookauthor = value; }
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值