积极答复者
关于getElementById()的返回值的问题,谢谢!

问题
-
我想请问一下:
我有个gridview,在
var GridView2 = document.getElementById("<%=GridView2.ClientID %>");中
这个GridView2 代表什么??
在js中能获得这个gridview的所有行么??
要是可以获得该怎么写呢??谢谢!!
怎么老有不会的??- 已移动 肖小勇Moderator 2009年7月17日 2:06 (发件人:.NET Framework 一般性问题讨论区)
答案
-
GridView 生成到客户端最终会解释为 HTML 的 Table, 以下是以前写的导出 Excel 的,包含了遍历行和列----------------------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200906011053.aspx.cs" Inherits="Grid_X200906011053" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function ExportToExcel(fTableElement) { var fApplication; try { fApplication = new ActiveXObject("Excel.Application"); } catch (e) { alert("生成“Excel”需要安装“Excel”软件,请确主认您是否安装了“Excel”软件!并且允许使用 Active X"); return; } var fWorkbook = fApplication.Workbooks.Add(); var fWorkSheet = fWorkbook.ActiveSheet; var fRowCount = fTableElement.rows.length; var fColCount = fTableElement.rows[0].cells.length; for (i = 0; i < fRowCount; i++) { for (j = 0; j < fColCount; j++) { fWorkSheet.Cells(i + 1, j + 1).value = fTableElement.rows(i).cells(j).innerText; } } var fTable = document.getElementById("") for (i = 0; i < fTable.rows.length; i++) { for (j = 0; j < fTable.row[i].cells.length; j++) { for (k = 0; k < fTable.row[i].cells[i].children.length; k++) { alert(fTable.row[i].cells[i].children[k].value); } } } // range style fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).Interior.ColorIndex = 2; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).Borders.ColorIndex = 1; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).RowHeight = 16; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).Font.Name = "微软雅黑"; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).Font.Size = 10; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).HorizontalAlignment = -4108; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).VerticalAlignment = -4108; fApplication.Visible = true; } </script> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <input type="button" value="Export" onclick="ExportToExcel(document.getElementById('GridView1'));" /> </form> </body> </html>
知识改变命运,奋斗成就人生!- 已标记为答案 邹俊才Moderator 2009年7月17日 15:19
-
你好,
这个效果使用UpdatePanel + CheckBox 就可以实现!(都是服务器端控件,不是客户端的CheckBox)
jon.valett@gmail.com- 已标记为答案 邹俊才Moderator 2009年7月17日 15:19
全部回复
-
你好,
GridView2代表的是控件客户端ID值,ID与ClientID不同,有时,不能为控件分配唯一的名称。例如,如果 Repeater 控件在它的某个模板中包含一个 Label 控件,则为 Repeater 控件中的各个项呈现该 Label 控件的一个实例。在呈现控件的多个实例时,为防止出现命名冲突,ASP.NET 为页上的各个服务器控件自动生成一个唯一的 ClientID 值。ClientID 值是通过连接控件的 ID 值和它的父控件的 UniqueID 值生成的。如果未指定控件的 ID 值,则使用自动生成的值。生成的 ID 的各个部分以下划线字符 (_) 分隔。
Js可以获取,但这样实现比较麻烦,你说一下你的需求?
jon.valett@gmail.com -
GridView 生成到客户端最终会解释为 HTML 的 Table, 以下是以前写的导出 Excel 的,包含了遍历行和列----------------------------------------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="X200906011053.aspx.cs" Inherits="Grid_X200906011053" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function ExportToExcel(fTableElement) { var fApplication; try { fApplication = new ActiveXObject("Excel.Application"); } catch (e) { alert("生成“Excel”需要安装“Excel”软件,请确主认您是否安装了“Excel”软件!并且允许使用 Active X"); return; } var fWorkbook = fApplication.Workbooks.Add(); var fWorkSheet = fWorkbook.ActiveSheet; var fRowCount = fTableElement.rows.length; var fColCount = fTableElement.rows[0].cells.length; for (i = 0; i < fRowCount; i++) { for (j = 0; j < fColCount; j++) { fWorkSheet.Cells(i + 1, j + 1).value = fTableElement.rows(i).cells(j).innerText; } } var fTable = document.getElementById("") for (i = 0; i < fTable.rows.length; i++) { for (j = 0; j < fTable.row[i].cells.length; j++) { for (k = 0; k < fTable.row[i].cells[i].children.length; k++) { alert(fTable.row[i].cells[i].children[k].value); } } } // range style fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).Interior.ColorIndex = 2; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).Borders.ColorIndex = 1; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).RowHeight = 16; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).Font.Name = "微软雅黑"; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).Font.Size = 10; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).HorizontalAlignment = -4108; fWorkSheet.Range(fWorkSheet.Cells(1, 1), fWorkSheet.Cells(fRowCount, fColCount)).VerticalAlignment = -4108; fApplication.Visible = true; } </script> </head> <body> <form id="form1" runat="server"> <asp:GridView ID="GridView1" runat="server"> </asp:GridView> <input type="button" value="Export" onclick="ExportToExcel(document.getElementById('GridView1'));" /> </form> </body> </html>
知识改变命运,奋斗成就人生!- 已标记为答案 邹俊才Moderator 2009年7月17日 15:19
-
你好,
这个效果使用UpdatePanel + CheckBox 就可以实现!(都是服务器端控件,不是客户端的CheckBox)
jon.valett@gmail.com- 已标记为答案 邹俊才Moderator 2009年7月17日 15:19
-
你好,区别:
ID与ClientID不同,有时,不能为控件分配唯一的名称。例如,如果 Repeater 控件在它的某个模板中包含一个 Label 控件,则为 Repeater 控件中的各个项呈现该 Label 控件的一个实例。在呈现控件的多个实例时,为防止出现命名冲突,ASP.NET 为页上的各个服务器控件自动生成一个唯一的 ClientID 值。ClientID 值是通过连接控件的 ID 值和它的父控件的 UniqueID 值生成的。如果未指定控件的 ID 值,则使用自动生成的值。生成的 ID 的各个部分以下划线字符 (_) 分隔。
jon.valett@gmail.com