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