繁体   English   中英

在 grid.FindControl 期间出现错误“对象引用未设置为对象的实例”

[英]Error 'Object reference not set to an instance of an object' during grid.FindControl

有人在这里我的代码哪里出错了吗? 我在 .cs 文件下编写了以下代码来获取网格数:

    int totalCount = grid.FindControl("employee_to_rep").Controls.Count;
    for (int i = 0; i < totalCount; i++)
    {
        CheckBox ck = (CheckBox)grid.FindControl("employee_to_rep").Controls[i];
        HiddenField employeeIDValue = (HiddenField)grid.FindControl("employeeidToRep").Controls[i];
        if (ck.Checked)
        {
            test = employeeIDValue.Value.ToString();
        }
    }

但是当涉及到 (CheckBox)grid.FindControl("employee_to_rep").Controls[i];

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 80: 
Line 81:             int totalCount = grid.FindControl("employee_to_rep").Controls.Count;

有谁知道那里发生了什么?

aspx 文件中的代码:

    <tr>   
        <th class="graytext r">Add Reps to Team:</th>
        <td>               
         <asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
                DataSourceID="dsEmployees" AllowPaging="true" PageSize="1000" EnableViewState="false"
                GridLines="None" CssClass="clGridDirectory">
                <Columns>
                  <asp:TemplateField >
                    <ItemTemplate>
                      <asp:CheckBox runat="server" ID='employee_to_rep' Text='<%# Eval("fullname") %>'/> 
                      <asp:HiddenField runat="server" ID="employeeidToRep" Value='<%# Eval("employeeid") %>'/>
                      <asp:TextBox runat='server' ID='repID' Text='<%# Eval("rep_id") %>'/>
                    </ItemTemplate>
                  </asp:TemplateField>
                </Columns>
              </asp:GridView>       
           <asp:SqlDataSource ID="dsEmployees" runat="server" ConnectionString="<%$ ConnectionStrings:TestConnectionString %>"
                SelectCommand="app_staff_without_team_select" SelectCommandType="StoredProcedure">
          </asp:SqlDataSource>          
        </td>
    </tr>  

我认为您必须在GridViewRow上调用 FindControl 。

grid.Row[0].FindControl("employee_to_rep")

第一行。

grid.Row[grid.SelectedIndex].FindControl("employee_to_rep")

对于当前选定的行(如果选择了一行)

您可以在访问结果之前尝试检查 FindControl 的结果:

if (grid.FindControl("employee_to_rep") != null)

理想情况下,您将使用as运算符进行转换,然后在下一步中检查 null。

[编辑]基于其他答案...如果您正在遍历 gridview 行,则需要跳过 header 和页脚 roes 并仅检查数据行:

foreach (GridViewRow gvr in  GridView1.Rows)
{
    if (gvr.RowType == DataControlRowType.DataRow)
    {
        // do your thing
    }
}

我并没有真正使用 ASP,但这是我的猜测:

msdn 上的文档指出

只有当控件直接包含在指定容器中时,此方法才会找到控件; 也就是说,该方法不会在控件内的控件层次结构中进行搜索

employee_to_rep控件容纳在asp:TemplateFieldItemTemplate (或asp:GridView本身的其他容器之一)中可能会阻止FindControl方法按预期工作。

但是,文档最后说

有关如何在不知道其直接容器时查找控件的信息,请参阅如何:按 ID 访问服务器控件

在我看来,以下代码行正在搜索控件内的任何控件。

int totalCount = grid.FindControl("employee_to_rep").Controls.Count;

您可能需要稍微拆分一下上述行,以检查哪个部分是 null 参考的原因。 尝试:

var control = (CheckBox) grid.FindControl("employee_to_rep");

我怀疑上面的方法会起作用,但是它在定位的控件中找不到任何控件。

您可能可以使用递归方法来解决您的问题。 看看这个: http://msmvps.com/blogs/deborahk/archive/2009/07/27/finding-controls-on-forms.aspx

希望这可以帮助。

您可以使用GridViewRow

foreach (GridViewRow gvr in grid.Rows)
{
    CheckBox ck = (CheckBox)gvr.FindControl("employee_to_rep");
    HiddenField employeeIDValue = (HiddenField)gvr.FindControl("employeeidToRep");
    if (ck.Checked)
    {
        test = employeeIDValue.Value.ToString();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM