在 gridview(不是 datagridview)中获取点击的单元格索引 asp.net
Get clicked cell index in gridview (not datagridview) asp.net
当我在 gridview(不是 datagridview)中单击单元格时,我想获取单元格索引(不是行索引),而不是行索引。我使用 asp.net - c#
使用 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[=10=]", 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
是一个隐藏字段,以声明方式添加到 aspx 上以存储所选索引:
<asp:HiddenField ID="SelectedGridCellIndex" runat="server" Value="-1" />
<asp:GridView ID="GridView1" runat="server"
OnRowCreated="OnRowCreated"
OnSelectedIndexChanged="SelectedIndexChanged"
OnRowDataBound="OnRowDataBound" AutoGenerateColumns="false">
<Columns>
.....
您需要为此页面禁用事件验证,否则 ASP.NET 抱怨:
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="Grid.aspx.cs" Inherits="CSharp_WebApp.Grid" %>
当我在 gridview(不是 datagridview)中单击单元格时,我想获取单元格索引(不是行索引),而不是行索引。我使用 asp.net - c#
使用 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[=10=]", 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
是一个隐藏字段,以声明方式添加到 aspx 上以存储所选索引:
<asp:HiddenField ID="SelectedGridCellIndex" runat="server" Value="-1" />
<asp:GridView ID="GridView1" runat="server"
OnRowCreated="OnRowCreated"
OnSelectedIndexChanged="SelectedIndexChanged"
OnRowDataBound="OnRowDataBound" AutoGenerateColumns="false">
<Columns>
.....
您需要为此页面禁用事件验证,否则 ASP.NET 抱怨:
<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="Grid.aspx.cs" Inherits="CSharp_WebApp.Grid" %>