【发布时间】:2020-06-21 06:44:43
【问题描述】:
我有一个gridview,它在页面加载时绑定数据:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindCustomerData();
}
}
我想根据条件显示不同的按钮。为简单起见
如果它是第一个 gridview 行,则显示“第一条记录”
如果是最后一个 gridview 行,则显示“Last Record”
任何其他行“中间记录”
所有按钮都以标记上的Visible = False 开头。
在我的RowDataBound 活动中,我有
protected void gvCusts_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
int lastIndex = gvCusts.Rows.Count -1;
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button buttonFirst = (Button)e.Row.FindControl("buttonFirst");
Button buttonMiddle = (Button)e.Row.FindControl("buttonMiddle");
Button buttonLast = (Button)e.Row.FindControl("buttonLast");
// Customer Records found
if (c.Id > 0)
{
if (e.Row.RowIndex == 0)
{
// First row so display first button only
buttonFirst.Visible = true;
}
else if (e.Row.RowIndex >= 1 && e.Row.RowIndex <= lastIndex)
{
// Not the first row and not the last row so display the middle button only
buttonMiddle.Visible = true;
}
else if (e.Row.RowIndex == lastIndex)
{
// Last row, so display the last button only.
buttonLast.Visible = true;
}
}
}
}
问题是我找不到正确的方法来获取中间行和最后一行,并且按钮显示在错误的阶段。
然后我创建了 int lastIndex = gvCusts.Rows.Count -1; 代码行 static ,但仍然没有得到正确的结果。
我已经阅读了很多文章,所以很明显我在某个地方遗漏了一些东西,但不确定是什么?
【问题讨论】:
-
lastIndex 计算错误,应该来自你的数据表。