.net 后台取js赋给hidden的值

 

 <script type="text/javascript">
    //以“全选”复选框作为参数
    function checkAll(){
        var checkAll = document.getElementById("All");
        var checkItem = document.getElementsByTagName("input");
        for (var i = 0; i < checkItem.length; i++) {
            if(checkItem[i].type=="checkbox"){
                checkItem[i].checked = checkAll.checked;
            }
        }
    }

    //获取选中的复选框
    function Update() {       
         var chkItems = document.getElementsByName("Item");
            var bookId = "";           
            for (var i = 0; i < chkItems.length; i++) {              
                if (chkItems[i].checked) {
                    bookId += chkItems[i].value + ",";
                }
            }           
            bookId = bookId.substr(0, bookId.length - 1);          
            document.getElementById(‘<%=hidBookId.ClientID%>‘).value = bookId;
                //alert(document.getElementById(‘<%=hidBookId.ClientID%>‘).value);
        }
</script>

   

HTML代码:

<input type="checkbox" id="All" onclick="checkAll()"/>全选
<input type="checkbox" name="Item" value="<%# Eval("Id") %>" />

 

  <input type="hidden" id="hidBookId"  name="hidBookId" runat="server" />  
    将选中的书籍归类于:<asp:DropDownList ID="ddlCategory" runat="server"></asp:DropDownList>
  <asp:Button ID="btnCategory" runat="server" Text="修  改" OnClick="btnCategory_Click"  OnClientClick="Update();" />

 

后台代码: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;
public partial class ListBooksByCategory : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CategoryBind();
            BooksBind();        
        }      
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="pm"></param>
    protected void BooksBind()
    {
        List<Book> list = new BookManager().GetAllBooks();
        this.gvBooks.DataSource = list;
        this.gvBooks.DataBind();
    }

    /// <summary>
    /// 绑定图书类型名称
    /// </summary>
    protected void CategoryBind()
    {
        this.ddlCategory.DataSource = new CategoryManager().GetAllCategory();
        this.ddlCategory.DataTextField = "Name";
        this.ddlCategory.DataValueField = "Id";
        this.ddlCategory.DataBind();
    }

    protected void gvBooks_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //获取索引编号
        this.gvBooks.PageIndex = e.NewPageIndex;
        BooksBind();
    }

    /// <summary>
    /// 单击修改按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCategory_Click(object sender, EventArgs e)
    {        
       string strBookId = this.hidBookId.Value;
       string[] array = strBookId.Split(‘,‘);
       for (int i = 0; i < array.Count(); i++)
       {
           int categoryId = Convert.ToInt32(this.ddlCategory.SelectedValue);
           new BookManager().UpdateCategoryIdById(Convert.ToInt32(array[i]), categoryId);
       }      
       BooksBind();
    }
}

  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。