如何使用JavaScript在客户端的gridView中找到控件的值?

时间:2014-03-18 14:34:09

标签: javascript asp.net gridview

我有一个GridView,我想根据客户端使用javascript的id找到Control的值。我有点击的行索引。现在我想找到一个在同一行中有标签的列的值。

     function OnClientClickReplaceRewardRuleFile(link) {

            var row = link.parentNode.parentNode;
            var rowIndex = row.rowIndex - 1;
//            alert("RowIndex: " + rowIndex);
            var lbl = document.getElementById('<%=GridViewMultiplePOSAssociationId.Rows[rowIndex].FindControl("LabelStatusPendingPOSId").ClientID %>');
            alert(lbl);
            return false;
}

它不起作用。

aspx代码。

   <asp:TemplateField HeaderStyle-Width="80px" ItemStyle-Width="250px" ItemStyle-HorizontalAlign="Center"
                                       HeaderText="Action">
                                       <ItemTemplate>
                                           <asp:LinkButton ID="HyperLinkAssociate" CommandArgument='<%#Eval("POS Id") %>'
                                               CommandName="Associate" Text="Associate" runat="server" OnClientClick="return OnClientClickAssociateRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>/<asp:LinkButton
                                                   ID="HyperLinkReplace" CommandArgument='<%#Eval("POS Id") %>' CommandName="Replace"
                                                   Text="Replace" runat="server" OnClientClick="return OnClientClickReplaceRewardRuleFile(this);" CausesValidation="false"></asp:LinkButton>
                                       </ItemTemplate>
                                   </asp:TemplateField >

我试过这个:

 function OnClientClickReplaceRewardRuleFile(link) {
            var row = link.parentNode.parentNode;
        var rowIndex = row.rowIndex - 1;
        var table = document.getElementById('<%=GridViewMultiplePOSAssociationId.ClientID %>');

        var lbls = document.getElementsByTagName('label');
        var spans = document.getElementsByTagName('span');

        for (var i = 0; i < lbls.length; i++) {
            if (lbls[i].id.indexOf('LabelStatusPendingPOSId') > 0) {
                if (rowIndex == i) {
                    if (lbls[i].innerText == 'Applied') {
                        alert('Action cannot be completed for this POS.');
                        return false;
                    }
                    break;
                }

            }
        }

        for (var i = 0; i < spans.length; i++) {
            if (spans[i].id.indexOf('LabelStatusPendingPOSId') > 0) {
                if (rowIndex == i) {
                    if (spans[i].innerText == 'Applied') {
                        alert('Action cannot be completed for this POS.');
                        return false;
                    }
                    break;
                }
            }
        }
}

1 个答案:

答案 0 :(得分:0)

试试这个

  function OnClientClickReplaceRewardRuleFile(link) {
        var row = link.parentNode.parentNode;
        var table = document.getElementById('<%=GridView1.ClientID %>');

        var lbls = document.getElementsByTagName('label');
        var spans = document.getElementsByTagName('span');

        for (var i = 0; i < lbls.length; i++) {
            if (lbls[i].id.indexOf('LabelStatusPendingPOSId') > 0) {
                alert(lbls[i]);
                break;
            }
        }

        for (var i = 0; i < spans.length; i++) {
            if (spans[i].id.indexOf('LabelStatusPendingPOSId') > 0) {
                alert(spans[i]);
                break;
            }
        }

    }