2012-04-03 66 views
1

孩子的GridView这是我的代码保存复选框值,而分页,而是因为我与nested gridview工作我无法找到所需要的孩子gridview的控制如何找到在用户定义的函数

private void SaveCheckedValues() 
{ 
    ArrayList userdetails = new ArrayList(); 
    int index = -1; 
    GridView gv = (GridView)gvCustomers.FindControl("gvOrders"); // Is this correct or any other way of finding the child control 
    foreach (GridViewRow gvrow in gv.Rows) 
    { 
     index = (int)gv.DataKeys[gvrow.RowIndex].Value; 
     bool result = ((CheckBox)gvrow.FindControl("chkBoxChild")).Checked; 

     // Check in the Session 
     if (Session["CHECKED_ITEMS"] != null) 
      userdetails = (ArrayList)Session["CHECKED_ITEMS"]; 
     if (result) 
     { 
      if (!userdetails.Contains(index)) 
       userdetails.Add(index); 
     } 
     else 
      userdetails.Remove(index); 
    } 
    if (userdetails != null && userdetails.Count > 0) 
     Session["CHECKED_ITEMS"] = userdetails; 
} 

回答

0

我有一个通用的递归查找控制代码,经常有助于在这些情况下。网格控件的问题在于,对于行和单元格以及单元格中的内容,有一定程度的控件嵌套。

Private Function FindControlRecursive(ByVal root As Control, ByVal id As String) As Control 

    If root.ClientID Is Nothing AndAlso root.ClientID.EndsWith(id) Then 

     Return root 
    End If 

    For Each c As Control In root.Controls 

     Dim t As Control = FindControlRecursive(c, id) 
     If Not t Is Nothing Then 
      Return t 
     End If 

    Next c 

    Return Nothing 
End Function 

的代码是VB.net,但你得到的要点

0
private void SaveCheckedValues() 
{ 
    ArrayList userdetails = new ArrayList(); 
    int index = -1; 
    foreach (GridViewRow gvRow1 in gvCustomers.Rows) 
    { 
     GridView gv = (GridView)gvRow1.FindControl("gvOrders"); 

     foreach (GridViewRow gvrow in gv.Rows) 
     { 
      index = (int)gv.DataKeys[gvrow.RowIndex].Value; 
      bool result = ((CheckBox)gvrow.FindControl("chkBoxChild")).Checked; 

      // Check in the Session 
      if (Session["CHECKED_ITEMS"] != null) 
       userdetails = (ArrayList)Session["CHECKED_ITEMS"]; 
      if (result) 
      { 
       if (!userdetails.Contains(index)) 
        userdetails.Add(index); 
      } 
      else 
       userdetails.Remove(index); 
     } 
    } 
    if (userdetails != null && userdetails.Count > 0) 
     Session["CHECKED_ITEMS"] = userdetails; 
} 
0

试试这个:

private void SaveCheckedValues() 
{ 
    foreach(GridViewRow rIndex in GridView1.Rows) 
    { 
    GridView gv = new GridView(); 
    gv = (GridView)row.FindControl("GridView2"); 
    //user gv 
    } 
} 
相关问题