方法一:
使用Parent属性就可以
比如我们自定义了一个LinkButton,那么当我们点击这个控件时如何知道它在GridView中所在的行呢,请看下面的代码。
1 protected void LinkButton1_Command(object sender, CommandEventArgs e)
2 {
3 LinkButton lb = (LinkButton)sender;
4 DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;
5 GridViewRow gvr = (GridViewRow)dcf.Parent;
6 GridView1.SelectedIndex = gvr.RowIndex;
7 }
使用第一个Parent属性的时候就会将LinkButton的范围扩大到DataControlFieldCell(当前按钮所在表格中的列单元),然后再使用一次Parent属性,可以再将列单元的范围扩大到GridViewRow(当前列单元所在的行单元),这个时候使用行单元的RowIndex属性就可以得到当前行所在表格的序号了。
方法二:
在你的GridView的Databind事件里面写
if (e.Row.RowType == DataControlRowType.DataRow)
{ ((Button)e.Row.FindControl("button7")).Attributes.Add("OnClick", "window.open('../Care/EditCare.aspx?CareId=" + ((Label)e.Row.FindControl("Label8")).Text + "&a=" + Request["a"].ToString() + "','_blank','height=500,width=795,top=180,left=220,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no')");
}
方法三:
protected void Button7_Click(object sender, EventArgs e)
{
int selectedIndex = 65533;
for (int i = 0; i < GridView1.Rows.Count; i++)
{
if (((GridView1.Rows[i].FindControl("button7"))) == sender)
{
selectedIndex = i;
}
}
if (selectedIndex != 65533)
{
string CareId = ((Label)GridView1.Rows[selectedIndex].FindControl("Label8")).Text;
Response.Write("<script language='javascript' type=''>window.open('../Care/EditCare.aspx?CareId=" + CareId + "&a=" + Request["a"].ToString() + "','_blank','height=500,width=795,top=180,left=220,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no')</script>");
}
}