日期:2014-05-19  浏览次数:20355 次

gridview如何获取索引值问题
我定义了   模版列  
为连接列     我想获取当先   点击字段的所在行   应如何表达啊?
编号   医生姓名   星期二   星期三   星期四   星期五   星期六   星期日   星期一  
    1         医生A       1             1        
    2         医生B       1                               1           1    
    3         医生C       1                                           1    


当我点击   1的时候   能   获得该行的   所在位置   利于我为下一个页面传递参数

比如我点     第三行   星期二下面的那个   1   能   获取   是医生C   这个值   或者   编号里面的那个值   都可以

------解决方案--------------------
使用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属性就可以得到当前行所在表格的序号了。
------解决方案--------------------
2个方法
第一个
在你的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 ') ");
}

这样~这个按钮就能把你这个GirdView这行指定的控件值传递到下一个页面~~推荐这么写

第2个
手动自己查找索引

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> ");
}
}
都是我曾经写过的代码~肯定好用~