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