[英]Get clicked cell index in gridview (not datagridview) asp.net
When I click cell in gridview (not datagridview) I want to get cell index (not row index), not row index. 当我单击gridview(不是datagridview)中的单元格时,我想获取单元格索引(而不是行索引),而不是行索引。 I use asp.net - c# 我使用asp.net-C#
Use the RowCreated
event to register a cell-click on each cell and handle the GridView's
SelectedIndexChangedEvent
: 使用RowCreated
事件在每个单元格上单击一个单元格并处理GridView's
SelectedIndexChangedEvent
:
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++ )
{
TableCell cell = e.Row.Cells[i];
cell.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
cell.Attributes["onmouseout"] = "this.style.textDecoration='none';";
cell.ToolTip = "You can click this cell";
cell.Attributes["onclick"] = string.Format("document.getElementById('{0}').value = {1}; {2}"
, SelectedGridCellIndex.ClientID, i
, Page.ClientScript.GetPostBackClientHyperlink((GridView)sender, string.Format("Select${0}", e.Row.RowIndex)));
}
}
}
protected void SelectedIndexChanged( Object sender, EventArgs e)
{
var grid = (GridView) sender;
GridViewRow selectedRow = grid.SelectedRow;
int rowIndex = grid.SelectedIndex;
int selectedCellIndex = int.Parse(this.SelectedGridCellIndex.Value);
}
SelectedGridCellIndex
is a hidden-field which is added declaratively on the aspx to store the selected index: SelectedGridCellIndex
是一个隐藏字段,在aspx上以声明方式添加该字段以存储所选索引:
<asp:HiddenField ID="SelectedGridCellIndex" runat="server" Value="-1" />
<asp:GridView ID="GridView1" runat="server"
OnRowCreated="OnRowCreated"
OnSelectedIndexChanged="SelectedIndexChanged"
OnRowDataBound="OnRowDataBound" AutoGenerateColumns="false">
<Columns>
.....
You need to disable event-validation for this page, otherwise ASP.NET complains: 您需要为此页面禁用事件验证,否则ASP.NET会抱怨:
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="Grid.aspx.cs" Inherits="CSharp_WebApp.Grid" %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.