2012-04-03 6 views
1

Dies ist mein Code der ausgewählten Kontrollkästchen Werte während Paging zu speichern, aber wie ich mit nested gridview arbeite ich bin nicht in der Lage, die Kontrolle über das erforderliche Kind gridview findenWie Kind gridview in Benutzern finden definierte Funktion

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; 
} 

Antwort

0

Ich habe eine generische rekursive Suche Steuercode, der oft unter diesen Umständen hilft. Das Problem mit Grid-Steuerelementen besteht darin, dass die Anzahl der Steuerelemente für die Zeile und die Zelle sowie der Inhalt in der Zelle verschachtelt sind.

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 

Der Code ist in VB.net, aber Sie erhalten den Kern

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

try this:

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