根据我的研究,为了在 ASP.NET 动态数据中获取事件GridView's
单元格的值,PreRender
您需要执行以下操作:
FieldTemplate
在单元格的控件集合中找到
- 强制
FieldTemplate
转换FieldTemplateUserControl
以访问它的属性
我刚刚根据我的项目模拟了您的问题。
列表.aspx.cs:
protected void gvOffices_PreRender(object sender, EventArgs e)
{
for (int rowIndex = gvOffices.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gvOffices.Rows[rowIndex];
GridViewRow previousRow = gvOffices.Rows[rowIndex + 1];
if (row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < row.Cells.Count; i++)
{
string dataField = ((DynamicField)((DataControlFieldCell)row.Cells[i]).ContainingField).DataField;
Control dataControl = ((FieldTemplateUserControl)((DynamicControl)row.Cells[i].FindDynamicControlRecursive(dataField)).FieldTemplate).DataControl;
string cellText = ((Literal)dataControl).Text; // for text fields
string dataFieldPrev = ((DynamicField)((DataControlFieldCell)previousRow.Cells[i]).ContainingField).DataField;
Control dataControlPrev = ((FieldTemplateUserControl)((DynamicControl)previousRow.Cells[i].FindDynamicControlRecursive(dataField)).FieldTemplate).DataControl;
string cellTextPrev = ((Literal)dataControl).Text; // for text fields
Response.Write(cellText);
Response.Write(cellTextPrev);
//if (cellText == cellTextPrev)
//{
// row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
// previousRow.Cells[i].RowSpan + 1;
// previousRow.Cells[i].Visible = false;
//}
}
}
}
}
FindDynamicControlRecursive()
您可以在http://csharpbits.notaclue.net/2009/01/dynamic-data-cascading-fieldtemplates.html找到。
在您的项目中考虑这种技术。我希望这将有所帮助。
编辑1:
我们还需要检查 GridView 单元格内的控件是否为HyperLink
or Literal
:
if (dataControl is Literal)
{
cellText = ((Literal)dataControl).Text;
}
else
{
if (dataControl is HyperLink)
{
cellText = ((HyperLink)dataControl).Text;
}
}
编辑2:(对我来说很好)
检查 GridView 单元格内的控件是否为DynamicField
:
protected void gvOffices_PreRender(object sender, EventArgs e)
{
for (int rowIndex = gvOffices.Rows.Count - 2; rowIndex >= 0; rowIndex--)
{
GridViewRow row = gvOffices.Rows[rowIndex];
GridViewRow previousRow = gvOffices.Rows[rowIndex + 1];
if ((row.RowType == DataControlRowType.DataRow) && (previousRow.RowType == DataControlRowType.DataRow))
{
for (int i = 0; i < row.Cells.Count; i++)
{
// check for the current row
if (((DataControlFieldCell)row.Cells[i]).ContainingField is DynamicField)
{
string dataField = ((DynamicField)((DataControlFieldCell)row.Cells[i]).ContainingField).DataField;
Control dataControl = ((FieldTemplateUserControl)((DynamicControl)row.Cells[i].FindDynamicControlRecursive(dataField)).FieldTemplate).DataControl;
string cellText = string.Empty;
if (dataControl is Literal)
{
cellText = ((Literal)dataControl).Text;
}
else
{
if (dataControl is HyperLink)
{
cellText = ((HyperLink)dataControl).Text;
}
}
// get cells text of the current row
Response.Write(cellText);
}
// check for the previous row
if (((DataControlFieldCell)previousRow.Cells[i]).ContainingField is DynamicField)
{
string dataFieldPrev = ((DynamicField)((DataControlFieldCell)previousRow.Cells[i]).ContainingField).DataField;
Control dataControlPrev = ((FieldTemplateUserControl)((DynamicControl)previousRow.Cells[i].FindDynamicControlRecursive(dataFieldPrev)).FieldTemplate).DataControl;
string cellTextPrev = string.Empty;
if (dataControlPrev is Literal)
{
cellTextPrev = ((Literal)dataControlPrev).Text;
}
else
{
if (dataControlPrev is HyperLink)
{
cellTextPrev = ((HyperLink)dataControlPrev).Text;
}
}
// get cells text of the previous row
Response.Write(cellTextPrev);
}
// Try to merge cells
//if (cellText == cellTextPrev)
//{
// row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 :
// previousRow.Cells[i].RowSpan + 1;
// previousRow.Cells[i].Visible = false;
//}
}
}
}
}