如何在javascript中获取gridview中新输入的值

时间:2017-08-14 13:02:22

标签: javascript c# asp.net gridview

我正在开发一个项目,并使用gridview查看一些可编辑的数据。我对这些数据有一些约束,我想在将数据发送到数据库之前执行前端检查。我的代码如下:

<asp:Button ID="Update" runat="server" CausesValidation="True" 
            CommandName="Update" Text="Update" onclientClick="check(this)"/>

我的JavaScript如下:

function check(up) {

    var ip, desc, rt;

    var table = document.getElementById('<%=GridView1.ClientID %>');

    var row = up.parentNode.parentNode;

    ip = row.cells[1].children[0].attributes[2].value;
    desc = row.cells[2].children[0].attributes[2].value;
    rt = row.cells[3].children[0].attributes[2].value;
    time = parseFloat(rt);
    alert(rt);

    if (ip === null || ip.match(/^ *$/) !== null) {
        alert('please enter IP Address');
        return false;
    }
    else if (desc === null || desc.match(/^ *$/) !== null) {
        alert('please enter description');
        return false;
    }
    else if (rt === null || rt.match(/^ *$/) !== null) {
        alert('please enter refresh time');
        return false;
    }
    else if (isNaN(time)){
        alert('refresh time must be a number');
        return false;
    }
    else if (Number(time) < 1) {
        alert('the minimum refresh time is 1');
        return false;
    }
    else 
        return true;
}

问题在于我总是得到旧值而不是新值。如何在JavaScript中获取新值? 我在更新查询中使用了sqlDataSource

UPDATE Clients
SET ipAddress = @ipAddress, description = @description, refreshTime = @refreshTime
WHERE machineName = @MachineName

1 个答案:

答案 0 :(得分:0)

您没有从行中获取行索引,请尝试以下操作:

var table = document.getElementById('<%=GridView1.ClientID %>');

var row = up.parentNode.parentNode;

var rindex = row.rowIndex; // get gridview row index

ip = table.rows[rindex].cells[1].children[0].value;
desc = table.rows[rindex].cells[2].children[0].value;
rt = table.rows[rindex].cells[3].children[0].value;