2012-04-03 3 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 
    } 
}