2

首先,是否可以在客户端使用JavaScript循环遍历asp:GridView中的数据。如果是,我想知道该怎么做...

因为我打算捕获 Grid 上 2 个字段的值,并根据它们的值在每一行上附加一个图像(一旦我可以遍历数据并比较它们,那部分就不应该造成太大问题)。

我的网格看起来像这样

 <asp:GridView ID="GridView1" runat="server" EmptyDataText="No Record Found" AllowSorting="true"
        AllowPaging="false" OnRowCreated="GridView1_RowCreated" Visible="true" BackColor="ButtonShadow"
        OnSorting="GridView1_Sort" AutoGenerateColumns="false" GridLines="None" >
        <Columns>
            </asp:TemplateField>
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
            <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
            <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
            <asp:BoundField DataField="Exam" HeaderText="Exam" SortExpression="Exam" />
            <asp:BoundField DataField="DateTime" HeaderText="DateTime" SortExpression="DateTime" />
            <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
            <asp:BoundField DataField="Priority" HeaderText="Priority" SortExpression="Priority" />
        </Columns>
    </asp:GridView>

如果我直截了当地说:我如何捕捉每一行的性别年龄?

PS我也对JQuery解决方案持开放态度............

4

2 回答 2

5

由于您拥有idgridview,您可以在 Javascript 中获取元素,遍历其行,然后遍历行的单元格,检查索引 1(性别)或 2(年龄),并获取它innerHTML以获取内容。看看这个:

window.onload = function () {
    var table, tbody, i, rowLen, row, j, colLen, cell;

    table = document.getElementById("GridView1");
    tbody = table.tBodies[0];

    for (i = 0, rowLen = tbody.rows.length; i < rowLen; i++) {
        row = tbody.rows[i];
        for (j = 0, colLen = row.cells.length; j < colLen; j++) {
            cell = row.cells[j];
            if (j == 1) {
                // Gender
                console.log("--Found gender: " + cell.innerHTML);
            } else if (j == 2) {
                // Age
                console.log("--Found age: " + cell.innerHTML);
            }
        }
    }
};

演示:http: //jsfiddle.net/ZRXV3/

这绝对取决于您呈现的 HTML 的结构。可能总是有一个隐藏的列或其他东西。


更新:

一个 jQuery 解决方案可能是这样的:

$(document).ready(function () {
    var rows, genders, ages;

    rows = $("#GridView1").children("tbody").children("tr");    // Find all rows in table (content)
    genders = rows.children("td:nth-child(2)");    // Find all columns that are the 2nd child
    ages = rows.children("td:nth-child(3)");    // Find all columns that are the 3rd child

    // An example of looping through all "gender" columns
    genders.each(function () {
        console.log($(this).html());
    });
});

演示:http: //jsfiddle.net/YgY35/

于 2012-10-17T15:10:37.100 回答
0

我期待着同样的解决方案,我找到了上面的答案。实施后,它在 IE 11 中不起作用。

我修改了解决方案并在 IE 11 中也工作。

var rOrders = new Array();
var table, tbody, i, rowLen, row, j, colLen, cell, tr;
var result = false;

table = document.getElementById("<%=gvRunningOrder.ClientID%>");
tbody = table.tBodies[0];
tr = $(tbody).find('tr');

for (i = 0, rowLen = tr.length; i < rowLen; i++) {
    row = tr[i];
    for (j = 0, colLen = row.cells.length; j < colLen; j++) {
        cell = row.cells[j];
        if (j == 1) {
            rOrders.push(cell.childNodes[1].value);
        }
    }
}
于 2018-10-25T09:25:54.193 回答