gridview中cell的选择和编辑

本文介绍如何在ASP.NET中使用GridView控件,并通过JavaScript实现单元格点击事件。具体方法是在aspx页面中定义一个JavaScript函数cellClicked,用于接收行和列的参数并在点击时触发。同时,在aspx.cs文件中设置GridView的RowDataBound事件,为每个单元格添加onclick属性,调用定义好的JavaScript函数。

aspx中定义一个js函数:
<script type="text/javascript">
        function cellClicked(row, column){
myForm.inhAction.value = "CELLCLICKED";
myForm.inhRow.value = row;
myForm.inhColumn.value = column;
myForm.submit();
// alert(row);
}
    </script>
其中aspx中form名称为"myForm".
添加三个隐藏域:   <input runat="server" type="hidden" id="inhAction" />
        <input runat="server" type="hidden" id="inhRow" />
        <input runat="server" type="hidden" id="inhColumn" />

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)// && (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal))
        {
            for (int i = 0; i < GridView1.Columns.Count; i++)
            {
                e.Row.Cells[i].Attributes["onclick"] = String.Format("cellClicked({0},{1})", e.Row.RowIndex, i);
            }
        }
    }
在aspx.cs代码中定义gridview的rowDataBound事件:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            for (int i = 0; i < GridView1.Columns.Count; i++)
            {
                e.Row.Cells[i].Attributes["onclick"] = String.Format("cellClicked({0},{1})", e.Row.RowIndex, i);
            }
        }
    }
当点击cell时,即执行定义的js函数, 并传入相关的行和列值.

  
测试页面下载: /Files/margiex/TestCellSelect.rar


参考文章:
http://www.cnblogs.com/webabcd/archive/2007/04/22/723113.html
http://www.webforumz.com/lo-fi/t-25121.html
http://www.codeproject.com/useritems/EditGridviewCells.asp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值