2012-02-15 4 views
0

두 번째 눈금 ("StaffAppraisalGrid")을 제어하는 ​​데 문제가 있습니다. 내가 코드를 실행할 때마다 null을 보여줍니다. GridView staffGrid = (GridView) SubmitAppraisalGrid.FindControl ("StaffAppraisalGrid"); . 무엇이 문제 일 수 있습니까?Gridview 내에서 템플릿 gridview의 컨트롤을 찾는 방법은 무엇입니까?

디자인 :

<asp:GridView ID="SubmitAppraisalGrid" runat="server" AutoGenerateColumns="False" ShowHeader="False" BorderWidth="0px"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:Label ID="QuestionLbl" runat="server" Text='<%# Bind("Question")%>'></asp:Label> 
       <asp:GridView ID="StaffAppraisalGrid" runat="server" AutoGenerateColumns="False"> 
        <Columns> 
         <asp:BoundField DataField="StaffName"/> 
         <asp:TemplateField> 
          <ItemTemplate> 
           <asp:RadioButtonList ID="RadioList" runat="server" DataSource='<%# Bind("RadioButtonList")%>'> 
           </asp:RadioButtonList> 
          </ItemTemplate> 
         </asp:TemplateField> 
         <asp:TemplateField> 
          <ItemTemplate> 
           <asp:TextBox ID="RemarksTbx" runat="server" Text='<%# Bind("RemarkTbx")%>'></asp:TextBox> 
          </ItemTemplate> 
         </asp:TemplateField> 
        </Columns> 
       </asp:GridView> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

샘플 코드 :

   ArrayList listofquestion= dbmanager.GetAllQuestion(); 

      ArrayList listofstaff = dbmanager.GetAllStaffName(); 
      DataTable dt = new DataTable(); 
      DataRow dr = null; 
      dt.Columns.Add(new DataColumn("Question", typeof(string))); 

      foreach (Question ques in listofquestion) 
      { 
       dr = dt.NewRow(); 
       dr["Question"] = "Q"+ques.QuestionID + ") " + ques.QuestionDetails+"?"; 
       dt.Rows.Add(dr); 
      } 

      ViewState["QuestionTable"] = dt; 

      //staffgrid 
      int index = 0; 
      for (int i = 0; i < dt.Rows.Count; i++) 
      { 
       DataTable staffdt = new DataTable(); 
       DataRow staffdr = null; 
       staffdt.Columns.Add(new DataColumn("StaffName", typeof(string))); 
       staffdt.Columns.Add(new DataColumn("RadioButtonList", typeof(ArrayList))); 
       staffdt.Columns.Add(new DataColumn("RemarkTbx", typeof(string))); 
       ArrayList listofradio = new ArrayList(); 
       listofradio.Add("1"); 
       listofradio.Add("2"); 
       listofradio.Add("3"); 
       listofradio.Add("4"); 
       listofradio.Add("5"); 
       listofradio.Add("6"); 
       listofradio.Add("7"); 
       listofradio.Add("N/A"); 

       foreach (string staffname in listofstaff) 
       { 
        staffdr = staffdt.NewRow(); 
        staffdr["StaffName"] = staffname; 
        staffdr["RadioButtonList"] = listofradio; 
        staffdr["RemarkTbx"] = string.Empty; 
        staffdt.Rows.Add(staffdr); 
       } 
       GridView staffGrid = (GridView)SubmitAppraisalGrid.FindControl("StaffAppraisalGrid"); 
       ViewState["QuestionTable"] = staffdt; 
       staffGrid.DataSource = staffdt; 
       staffGrid.DataBind(); 
       index++; 
      } 
      //before bind 
      SubmitAppraisalGrid.DataSource = dt; 
      SubmitAppraisalGrid.DataBind(); 

답변

0

이 시도 때문에 아이 부모의 GridView의 한 셀 아래에있는 gridview. 당신이 액세스 할 때, 그것이 있어야 .. ParentGridView.Row [rowIdx] .FindControl

// 행 인덱스

GridView staffGrid = (GridView)SubmitAppraisalGrid.Row[0].FindControl("StaffAppraisalGrid"); 
+0

ermm 아니, 그것은 일을 해달라고. ArgumentOutOfRange 오류가 발생합니다. –

+0

Emm .. 루핑하고 자식 gridview를 찾는 코드를 proivde 수 있습니까? –

0

이 코드를 시도

protected void GVmaster_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 


if (e.Row.RowType == DataControlRowType.DataRow) 
{ 
    GridView gvc = (GridView)e.Row.FindControl("gvChild"); 
} 

} 
관련 문제