插入列后Gridview控制器为空

问题描述:

我正在开发一个Web应用程序来管理学生考试条目,但我遇到了一个问题,我的gridview。插入列后Gridview控制器为空

的基本思路是,用户可以去和一个学生考试输入和更新的详细信息。当某些主题需要填充不同的字段时会出现并发症,这就是我的问题所在。运行保存按钮方法时

抛出异常。运行AddAudit和UpdateRecord方法时,会得到一个Null Object引用。在从外观上进行一些调试后,问题是控件(ddlDate,txtAssesmentLevel等)没有从GridView的FindCotrol方法中声明,这意味着当调用AddAudit和UpdateRecord方法时,它们指向一个空控制器。

要知道,当代码不是“C2555”,这使我怀疑问题是与动态添加列和控制越来越糊涂了,虽然我不能肯定不会出现这个问题。

任何援助将是巨大的,并随时要求更多信息。

下面是我的基本代码:

页面加载方法

protected void Page_Load(object sender, EventArgs e){ 

if(!IsPostBack){ 
//Selected class passed through 
    selectedClass sc = (selectedClass)Session["selectedClass"] as selectedClass; 

//Get Class Code 
    lblAosCode.Text = sc.getAOSCode(); 

//If class is English 
    if(lblAosCode.Text == "C2555"){ 
    TemplateField speakingListening = new TemplateField(); 
    speakingListening.HeaderText = "Speaking and Listening"; 
    dgvSelectedClasses.Columns.Insert(7, speakingListening); 
    } 


//Populate Gridview 
DataTable dsSelectedClasses = AccessData.getSelectedClasses(sc.getAOSCode(), sc.getAOSPeriod(), sc.getDescription());  
dgvSelectedClasses.DataSource = dsSelectedClasses; 
dgvSelectedClasses.DataBind(); 



//Check if txtAssesmentLevel is populated 
for (int index = 0; index < dgvSelectedClasses.Rows.Count; index++) 
      { 
      TextBox txtAssessmentLevel = (TextBox)dgvSelectedClasses.Rows[index].FindControl("txtAssessmentLevel"); 
       if (dgvBefore.Rows[index].Cells[4].Text != " ") 
       { 
        txtAssessmentLevel.ReadOnly = true; 
       } 
      } 
} 


} 

保存方法(抛出异常)

protected void btnSave_Click(object sender, EventArgs e) 
    { 



     for (int i = 0; i < dgvSelectedClasses.Rows.Count; i++) 
     { 
      DropDownList ddlL1L2 = (DropDownList)dgvSelectedClasses.Rows[i].FindControl("ddlL1L2"); 
      DropDownList ddlExamDate = (DropDownList)dgvSelectedClasses.Rows[i].FindControl("ddlExamDate"); 
      TextBox txtAssessmentLevel = (TextBox)dgvSelectedClasses.Rows[i].FindControl("txtAssessmentLevel"); 
      DropDownList ddlSpeakingListening = null; 
      if (lblAosCode.Text.Contains("C2555")) 
      { 
       ddlSpeakingListening = (DropDownList)dgvSelectedClasses.Rows[i].FindControl("ddlSpeakingListening"); 
      } 




      if (IsPostBack) 
      { 
       if (lblAosCode.Text.Contains("C2555")) 
       { 
        AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Assessment Level", txtAssessmentLevel.Text, dgvBefore.Rows[i].Cells[4].Text); 
        AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Exam request L1 or L2", ddlL1L2.SelectedValue, dgvBefore.Rows[i].Cells[14].Text); 
        AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Exam Date", ddlExamDate.SelectedValue, dgvBefore.Rows[i].Cells[15].Text); 
        AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Speaking and Listening", ddlSpeakingListening.SelectedValue, dgvBefore.Rows[i].Cells[7].Text); 

        AccessData.updateRecord(txtAssessmentLevel.Text, ddlL1L2.SelectedValue, ddlExamDate.SelectedValue, lblAosCode.Text, lblAosPeriod.Text, dgvSelectedClasses.Rows[i].Cells[0].Text, ddlSpeakingListening.SelectedValue); 
       } 
       else 
       { 
        AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Assessment Level", txtAssessmentLevel.Text, dgvBefore.Rows[i].Cells[4].Text); 
        AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Exam request L1 or L2", ddlL1L2.SelectedValue, dgvBefore.Rows[i].Cells[13].Text); 
        AccessData.addAudit(dgvSelectedClasses.Rows[i].Cells[0].Text, Context.User.Identity.Name, "Exam Date", ddlExamDate.SelectedValue, dgvBefore.Rows[i].Cells[14].Text); 

        AccessData.updateRecord(txtAssessmentLevel.Text, ddlL1L2.SelectedValue, ddlExamDate.SelectedValue, lblAosCode.Text, lblAosPeriod.Text, dgvSelectedClasses.Rows[i].Cells[0].Text); 
       } 
      } 

     } 

     Response.Redirect("~/contact"); 


    }  

在行数据绑定

protected void OnRowDataBound(object sender, GridViewRowEventArgs e) 
    { 

     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 

      if (lblAosCode.Text.Contains("C2555")) 
      { 
       DropDownList ddlSpeakingListening = new DropDownList(); 
       ddlSpeakingListening.ID = "ddlSpeakingListening"; 
       ddlSpeakingListening.Items.Add("L1"); 
       ddlSpeakingListening.Items.Add("L2"); 
       ddlSpeakingListening.Items.Add("Entry"); 
       ddlSpeakingListening.Items.Add("NS"); 
       e.Row.Cells[7].Controls.Add(ddlSpeakingListening); 




      } 

     } 
    }  

ASP。 NET

<asp:GridView ID="dgvSelectedClasses" runat="server" AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound"> 
      <Columns> 
       <asp:BoundField DataField="StudentID" HeaderText="Student ID" ReadOnly="True" /> 
       <asp:BoundField DataField="StageCode" HeaderText="Stage Code" ReadOnly="True" /> 
       <asp:BoundField DataField="Forename" HeaderText="Forename" ReadOnly="True" /> 
       <asp:BoundField HeaderText="Surname" DataField="Surname" /> 
       <asp:TemplateField HeaderText="Assessment Level"> 

        <ItemTemplate> 
         <asp:TextBox ID="txtAssessmentLevel" Text ='<%#Bind("AssessmetLevel") %>' runat="server" Width="50px"></asp:TextBox> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:BoundField DataField="TargetLevel" HeaderText="Target Level" /> 
       <asp:BoundField DataField="Achievelevel" HeaderText="Achieve Level" /> 
       <asp:BoundField DataField="FELSOutcome" HeaderText="FELS Outcome" /> 
       <asp:BoundField DataField="Registration" HeaderText="Registration" /> 
       <asp:BoundField DataField="DateSpreadsheetSent" HeaderText="Last Update" /> 
       <asp:BoundField DataField="Dayofclass" HeaderText="Day of class" /> 
       <asp:BoundField DataField="Timeofclass" HeaderText="Time of class" /> 
       <asp:BoundField DataField="Location" HeaderText="Location" /> 
       <asp:TemplateField HeaderText="Exam request L1 or L2" > 
        <ItemTemplate> 
         <asp:DropDownList ID="ddlL1L2" runat="server" > 
          <asp:ListItem>Not Set</asp:ListItem> 
          <asp:ListItem>L1</asp:ListItem> 
          <asp:ListItem>L2</asp:ListItem> 

         </asp:DropDownList> 
        </ItemTemplate> 
       </asp:TemplateField> 

       <asp:TemplateField HeaderText="Exam date"> 
        <ItemTemplate> 
         <asp:DropDownList ID="ddlExamDate" runat="server"> 

         </asp:DropDownList> 
        </ItemTemplate> 
       </asp:TemplateField> 

      </Columns> 
     </asp:GridView> 


<asp:GridView ID="dgvBefore" runat="server" AutoGenerateColumns="False"> 
     <Columns> 
      <asp:BoundField DataField="StudentID" HeaderText="Student ID" ReadOnly="True" /> 
      <asp:BoundField DataField="StageCode" HeaderText="Stage Code" ReadOnly="True" /> 
      <asp:BoundField DataField="Forename" HeaderText="Forename" ReadOnly="True" /> 
      <asp:BoundField HeaderText="Surname" DataField="Surname" /> 
      <asp:BoundField DataField="AssessmetLevel" HeaderText="Assessment Level" NullDisplayText=" "/> 
      <asp:BoundField DataField="TargetLevel" HeaderText="Target Level" /> 
      <asp:BoundField DataField="AchieveLevel" HeaderText="Achieve Level" /> 
      <asp:BoundField DataField="FELSOutcome" HeaderText="FELS Outcome" /> 
      <asp:BoundField DataField="Registration" HeaderText="Registration" /> 
      <asp:BoundField DataField="DateSpreadsheetSent" HeaderText="Date Spreadsheet Sent" /> 
      <asp:BoundField DataField="Dayofclass" HeaderText="Day of class" /> 
      <asp:BoundField DataField="Timeofclass" HeaderText="Time of class" /> 
      <asp:BoundField DataField="Location" HeaderText="Location" /> 
      <asp:BoundField DataField="ExamrequestL1orL2" HeaderText="Exam request L1 or L2" /> 
      <asp:BoundField DataField="Examdate" HeaderText="Exam date" /> 
      <asp:BoundField DataField="Reviewed" HeaderText="Reviewed" /> 
     </Columns> 
    </asp:GridView> 

附:这是我的第一个问题,希望它是有道理的,我很乐意指点:)

编辑:页面加载方法确实包含代码来填充dgvBefore以及一些认证的东西,我只是忘了包含它。

+1

詹姆斯,虽然你已经粘贴了大量的代码的问题不明确。你能否用确切的问题陈述来更新它? – A3006

+0

@ A3006问题是,当我按下保存按钮时,抛出了一个空对象引用异常。在Save方法开始时,GridView中的控件没有被分配到我使用查找控制方法的地方。仅当添加新的“ddlSpeakingListening”列时才会出现该错误。 –

我认为你需要在每个回传中添加下拉列表列。请参考以下链接:https://www.codeproject.com/Tips/682689/Add-populated-dropdownlist-to-GridView-dynamically