在js中取得gridView某行某列的值

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridViewSelectAll.aspx.cs"  
  2.     Inherits="bindData.gridViewSelectAll" %>  
  3.   
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.   
  9.     <script language="javascript" type="text/javascript">  
  10.         //全选  
  11.         function checkAll() {  
  12.             var checkBox = document.getElementsByTagName("input");  
  13.             for (var i = 0; i < checkBox.length; i++) {  
  14.                 if (checkBox[i].type == "checkbox") {  
  15.                     checkBox[i].checked = document.all.cb_title.checked;  
  16.                 }  
  17.             }  
  18.         }  
  19.   
  20.         //修改  
  21.         function editRow() {  
  22.             var checkCount = 0;  
  23.             var id = "";  
  24.             var gridView = document.getElementById("gridView_dataSet");  
  25.             var checkBox = document.getElementsByName("checkBox"); //取得gridView中所有的checkBox  
  26.             for (var i = 0; i < checkBox.length; i++) {  
  27.                 if (checkBox[i].type == "checkbox") {  
  28.                     if (checkBox[i].checked == true) {  
  29.                         id = gridView.rows[i + 1].cells[1].innerHTML; //取得gridView中某行的id  
  30.                         checkCount++;  
  31.                     }  
  32.                 }  
  33.             }  
  34.             if (checkCount != 1) {  
  35.                 alert("请选择一项进行修改!");  
  36.             } else {  
  37.                 alert("id=" + id);  
  38.             }  
  39.         }  
  40.   
  41.         function editRow1() {  
  42.             var id = "";  
  43.             var count = 0;  
  44.             var gridView = document.getElementById("gridView_dataSet");  
  45.             for (i = 1; i < gridView.rows.length; i++) {  
  46.                 var cb = gridView.rows(i).cells(0).children(0);  
  47.                 if (cb.checked) {  
  48.                     count++;  
  49.                     id = gridView.rows(i).cells(1).innerText;  
  50.                 }  
  51.             }  
  52.             if (count != 1) {  
  53.                 alert("请选择一项进行修改!");  
  54.             } else {  
  55.                 alert("id=" + id);  
  56.             }  
  57.         }  
  58.   
  59.     </script>  
  60.   
  61. </head>  
  62. <body>  
  63.     <form id="form1" runat="server">  
  64.     <div>  
  65.         <input type="button" value="选择一项" onclick="editRow();" />  
  66.         <input type="button" id="btn_edit" value="修改" onclick="editRow1();" />  
  67.         <asp:GridView ID="gridView_dataSet" runat="server">  
  68.             <Columns>  
  69.                 <asp:TemplateField>  
  70.                     <HeaderTemplate>  
  71.                         <input type="checkbox" id="cb_title" onclick="checkAll();">  
  72.                     </HeaderTemplate>  
  73.                     <ItemTemplate>  
  74.                         <input type="checkbox" id="cb_item" name="checkBox" />  
  75.                     </ItemTemplate>  
  76.                 </asp:TemplateField>  
  77.             </Columns>  
  78.         </asp:GridView>  
  79.     </div>  
  80.     </form>  
  81. </body>  
  82. </html>  


后台:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data;  
  8.   
  9. namespace bindData  
  10. {  
  11.     public partial class gridViewSelectAll : System.Web.UI.Page  
  12.     {  
  13.         protected void Page_Load(object sender, EventArgs e)  
  14.         {  
  15.             if (!IsPostBack)  
  16.             {  
  17.                 bindData();  
  18.             }  
  19.         }  
  20.   
  21.         /// <summary>  
  22.         /// 设置数据  
  23.         /// </summary>  
  24.         /// <returns></returns>  
  25.         private DataSet setData()  
  26.         {  
  27.             DataSet ds = new DataSet();//注:必须要new,否则会是未将对象设置到实例  
  28.   
  29.             DataTable dt = new DataTable();//设置表  
  30.   
  31.             DataColumn dc = null;//设置列  
  32.             dc = dt.Columns.Add("id"typeof(string));  
  33.             dc = dt.Columns.Add("goodsName"typeof(string));  
  34.             dc = dt.Columns.Add("price"typeof(string));  
  35.             dc = dt.Columns.Add("number"typeof(int));  
  36.             dc = dt.Columns.Add("buyTime"typeof(DateTime));  
  37.             dc = dt.Columns.Add("fresh"typeof(int));  
  38.   
  39.             DataRow dr = null;//设置行  
  40.             dr = dt.Rows.Add("1""a", 1.3, 1, "2005-10-2", 1);  
  41.             dr = dt.Rows.Add("tt""b", 0.5, 2, "2005-10-2", 0);  
  42.             dr = dt.Rows.Add("3""c", 1.5, 3, "2005-10-2", 1);  
  43.             dr = dt.Rows.Add("4""d", 0.2, 4, "2005-10-2", 0);  
  44.             dr = dt.Rows.Add("5""e", 0.2, 5, "2005-10-2", 0);  
  45.             dr = dt.Rows.Add("6""f", 1.6, 6, "2005-10-2", 1);  
  46.             dr = dt.Rows.Add("g""g", 0.3, 7, "2005-10-2", 0);  
  47.             dr = dt.Rows.Add("7""g", 0.3, 7, "2005-10-2", 0);  
  48.             dr = dt.Rows.Add("g""g", 0.3, 7, "2005-10-2", 0);  
  49.             dr = dt.Rows.Add("t""g", 0.3, 7, "2005-10-2", 0);  
  50.   
  51.             ds.Tables.Add(dt);  
  52.   
  53.             return ds;  
  54.         }  
  55.   
  56.   
  57.         /// <summary>  
  58.         /// 绑定数据源  
  59.         /// </summary>  
  60.         private void bindData()  
  61.         {  
  62.             gridView_dataSet.DataSource = setData();  
  63.             gridView_dataSet.DataBind();  
  64.         }  
  65.     }  
  66. }  

posted on 2011-08-03 14:11  骑着毛驴去编程  阅读(1155)  评论(0编辑  收藏  举报

导航