Does anyone where is my code going wrong here? I have wrote the following code under .cs file to get the grid count:
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();
}
}
But error display when it come to the line (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;
Does anyone know what is it going on there?
code in the aspx file:
<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>
I think you’ll have to call FindControl on a
GridViewRow.for first row.
for the currently selected row (if a row is selected)