How to access gridview cell values on client side

In this article I will be discussing how you can use client side javascript to access values in specified row and column of GridView control without doing any post back. I have discussed in my previous articles about highlighting rows and raising click event in selected row of grid view control, ref to How to highlight gridview row when row is selected and How to raise gridview server side event

The technique to access cell values will build upon the technique that we used for highlighting rows and raising server side event. You can refer to same code from those articles to see how client side onclick event was handled for each selected row. Javascript code in those examples did not deal with cell values. But there are cases when you need to check what values you have in various cells of the grid view and make decision on client side. For example if a user enters a data in some cell but you need to validate it against value in some other cell or combination of cells, you may have been doing a post back and then doing the checks and failing user attempt if something was wrong with the data. But by using this technique you can avoid the post back. 

<script language="javascript" type="text/javascript">
var gridViewCtlId = '<%=ctlGridView.ClientID%>';
    var gridViewCtl = null;
    var curSelRow = null;
    var curRowIdx = -1;
    function getGridViewControl()
    {
        if (null == gridViewCtl)
        {
            gridViewCtl = document.getElementById(gridViewCtlId);
        }
    }
    
    function onGridViewRowSelected(rowIdx)
    {
        var selRow = getSelectedRow(rowIdx);
        if (null != selRow)
        {
            curSelRow = selRow;
            var cellValue = getCellValue(rowIdx, 0);
            alert(cellValue);
        }
    }
    
    function getSelectedRow(rowIdx)
    {
        return getGridRow(rowIdx);
    }
    
    function getGridRow(rowIdx)
    {
        getGridViewControl();
        if (null != gridViewCtl)
        {
            return gridViewCtl.rows[rowIdx];
        }
        return null;
    }
    
    function getGridColumn(rowIdx, colIdx)
    {
        var gridRow = getGridRow(rowIdx);
        if (null != gridRow)
        {
            return gridRow.cells[colIdx];
        }
        return null;
    }
    
    function getCellValue(rowIdx, colIdx)
    {
        var gridCell = getGridColumn(rowIdx, colIdx);
        if (null != gridCell)
        {
            return gridCell.innerText;
        }
        return null;
    }
</

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Netkiller

怎样制作RPM包

怎样制作RPM包 摘要 我在网上找RPM包的制作例子几乎都是C源码编译安装然后生成RPM包, 而我的程序不是C写的很多时候是脚本语言如Python, PHP 甚...

79760
来自专栏移动开发之家

Android蹲坑的疑难杂症集锦(兼Gradle) 二

 这次我们继续聊聊项目中遇到的问题吧,作为上辈子的天使,才换来今生的入坑Android,码农不易,我们的梦想就是,下辈子下班时,不再月上柳梢头,可以看到夕阳下的...

12040
来自专栏Kubernetes

Controlling Access to the Kubernetes API

? API Server Ports and IPs By default the Kubernetes API server serves HTTP o...

30180
来自专栏刘望舒

Android深入四大组件(七)Android8.0 根Activity启动过程(后篇)

前言 在几个月前我写了Android深入四大组件(一)应用程序启动过程这篇文章,它是基于Android 7.0的,当我开始阅读Android 8.0源码时发现应...

289100
来自专栏散尽浮华

git review报错一例

在线上修改代码,最后使用git review提交代码审核的时候出现报错如下: [wangshibo@115~]$ vim testfile           ...

19870
来自专栏码匠的流水账

关于ribbonClient配置的一个坑

不知道从哪个版本起,给ribbon配置物理的server list起,单纯配置xxx.ribbon.listOfServers不起效果了,于是就开启了埋坑之旅。

47520
来自专栏ml

hdu----(4301)Divide Chocolate(状态打表)

多校综合排名前25名的学校请发送邮件到HDUACM@QQ.COM,告知转账信息(支付宝或者卡号) Divide Chocolate Time Lim...

36690
来自专栏微信音视频小程序

教你1天搭建自己的“微视”

A simple iOS Application project is shown below to illustrate how to configure S...

1K50
来自专栏Objective-C

iOS-安装和使用 CocoaPods

55570
来自专栏菩提树下的杨过

spring-boot 速成(8) 集成druid+mybatis

spring-boot与druid、mybatis集成(包括pageHelper分页插件), 要添加以下几个依赖项: compile('mysql:my...

95690

扫码关注云+社区

领取腾讯云代金券