使用JavaScript通过ASP循环:GridView数据



首先,甚至有可能使用 aT asp:gridview 中的数据循环> javascript。如果是,我想知道如何做...

当我打算捕获网格上2个字段的值时,并根据其值将图像连接到每行(,一旦我可以循环浏览数据,该部分就不会构成太多问题并比较它们)。

我的网格看起来像 this

 <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>

如果我直接宣传:如何捕获每行的性别和 age

p.s。我开放 jQuery 解决方案.......

由于您具有GridView的id,因此您可以在JavaScript中抓住元素,遍历行,然后是行的单元格,检查索引1(性别)或2(性别)或2(gender)或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/

我期待着相同的解决方案,我在上面找到了答案。实施时,它在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);
        }
    }
}

最新更新