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