开头
好久没有写了,这次写的是一个很简单的方法,大概的样子就像下图,点击Display后需要得到GridiviewRow对象。

正文
很自然想到的第一个方法就是Parent属性,使用两个Parent就可以得到GridviewRow对象,第一个Parent得到的是DataControlFieldCell。
protected
void
btnDisplay_Command(
object
sender, CommandEventArgs e)
{
Button btnDisplay = sender as Button;
GridViewRow row = btnDisplay.Parent.Parent as GridViewRow;
}
{
Button btnDisplay = sender as Button;
GridViewRow row = btnDisplay.Parent.Parent as GridViewRow;
}
第二个方法就比较便利,直接使用NamingContainer,可以得到GridviewRow。
protected
void
btnDisplay_Command(
object
sender, CommandEventArgs e)
{
Button btnDisplay = sender as Button;
GridViewRow row = btnDisplay.NamingContainer as GridViewRow;
}
{
Button btnDisplay = sender as Button;
GridViewRow row = btnDisplay.NamingContainer as GridViewRow;
}
本文介绍了在ASP.NET中通过两种不同的方式来获取GridViewRow对象:一是利用Parent属性连续访问两次,二是直接使用NamingContainer属性。这两种方法适用于需要从按钮点击事件中获取所在行的情况。
741





