前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >How to access gridview cell values on client side

How to access gridview cell values on client side

作者头像
阿新
发布2018-04-12 17:17:24
6210
发布2018-04-12 17:17:24
举报
文章被收录于专栏:c#开发者

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. 

代码语言:javascript
复制
<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;
    }
</
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2009-02-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档