我在控制第二个网格时遇到了问题(“StaffAppraisalGrid”)。每当我运行代码时它都会显示null GridView staffGrid =(GridView)SubmitAppraisalGrid.FindControl(“StaffAppraisalGrid”); 。可能是什么问题?
设计
<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 :(得分:0)
试试这个,因为子gridview位于父GridView的一个单元格下。因此,当您访问时,它应该是.. ParentGridView.Row [rowIdx] .FindControl
//行索引
GridView staffGrid = (GridView)SubmitAppraisalGrid.Row[0].FindControl("StaffAppraisalGrid");
答案 1 :(得分:0)
试试此代码
protected void GVmaster_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridView gvc = (GridView)e.Row.FindControl("gvChild");
}
}