1

Issue: Adding a checkbox or other non-Label ItemTemplate field causes a null return on the FindControl method.

Normal behavior: Normally, when a user clicks edit, the EditItemTemplate fields become available and I'm able to use the FindControl method to get the Controls. Simple right?

protected void grdOppProducts_RowDataBound(object sender, GridViewRowEventArgs e) {

    if (e.Row.RowType == DataControlRowType.DataRow) {
        if (e.Row.RowState == DataControlRowState.Edit) {
            //only enters edit mode where checkbox control isn't available
            TextBox txtQuantity = (TextBox)e.Row.FindControl("txtQuantity");
        }
        ...

Cause: Now, when adding a checkbox ItemTemplate field into the mix, the gridView rowState never reaches Edit mode, therefore the EditItemTemplate controls are never available resulting in null values.

The gridView control is defined as follows (the checkBox field, cbxSelect, is causing the issue)

<asp:GridView runat="server" id="oppProdgrdView" gridlines="None" AllowPaging="False" selectedrowstyle LightCyan="backcolor"
    autogeneratecolumns="false" 
    autoGenerateEditButton="false" 
    autoGenerateDeleteButton="true"
    OnRowDataBound="grdOppProducts_RowDataBound"
    OnRowCommand="grdOppProducts_RowCommand"
    OnRowUpdating="grdOppProducts_RowUpdating" 
    OnRowUpdated="grdOppProducts_RowUpdated"
    OnRowCancelingEdit="grdOppProducts_RowCancelingEdit"
    OnRowEditing="grdOppProducts_RowEditing" 
    OnRowDeleting="grdOppProducts_RowDeleting"
    cellpadding="1" cssclass="datagrid" pagerstyle-cssclass="gridPager"
alternatingrowstyle-cssclass="rowdk" rowstyle-cssclass="rowlt" selectedrowstyle-cssclass="rowSelected" showemptytable="True" enableviewstate="false"
emptytablerowtext="<%$ resources:dgOppProducts.EmptyTableRowText %>" expandablerows="True" resizablecolumns="True" currentsortdirection="Ascending" currentsortexpression="" 
datasourceid="SqlDataSource2" datakeynames="OPPPRODUCTID" showsorticon="False" useslxpagertemplate="True" AllowSorting="true" PageSize="16">

   <Columns>
       <asp:commandfield showeditbutton="true" causesvalidation="false" headertext=""/>

       <asp:BoundField DataField="OPPPRODUCTID" ItemStyle-CssClass="hidden-column" HeaderStyle-CssClass="hidden-column"
          HeaderText="OppProductId">
       </asp:BoundField>

       <asp:BoundField DataField="PRODUCTID" ItemStyle-CssClass="hidden-column" HeaderStyle-CssClass="hidden-column"
          HeaderText="ProductId">
       </asp:BoundField>

       <asp:TemplateField HeaderStyle-HorizontalAlign="Left"> 
            <ItemTemplate>
                <asp:CheckBox ID="cbxSelect" style="width: 10px;" runat="server" AutoPostBack="false" onClick="getCheckBoxValue($(this));" />
            </ItemTemplate>
       </asp:TemplateField>**

       <asp:TemplateField HeaderText="Line #" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtSort" style="width: 25px;" runat="server" AutoPostBack="false" Text='<%# Bind("SORT") %>'></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblSort" runat="server" Text='<%# Bind("SORT") %>'></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

       <asp:TemplateField HeaderText="Description" HeaderStyle-HorizontalAlign="Left"> 
           <ItemTemplate> 
            <asp:Label ID="lblName" runat="server" Text='<%# Bind("PRODUCTNAME") %>'></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

       <asp:TemplateField HeaderText="Version">
           <ItemTemplate>
                <asp:Label Text='<%# ACDGetVersion(Eval("PRODUCTID").ToString())%>'
                    runat="server" />
           </ItemTemplate>
       </asp:TemplateField>

       <asp:TemplateField HeaderText="Prorate">
           <EditItemTemplate> 
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
           </EditItemTemplate> 
           <EditItemTemplate>
                <asp:TextBox ID="txtPeriodAdjustment" style="width: 25px;" autopostback="false" Text='<%#ACDGetPeriodAdjustment(Eval("OPPPRODUCTID").ToString()) %>'
                    runat="server" />
           </EditItemTemplate>
           <ItemTemplate>
                <asp:Label ID="txtPer2" Text='<%#ACDGetPeriodAdjustment(Eval("OPPPRODUCTID").ToString()) %>'
                    runat="server" />
           </ItemTemplate>
       </asp:TemplateField>

       <asp:TemplateField HeaderText="Qty" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtQuantity" style="width: 25px;" runat="server" autopostback="false" Text='<%# Bind("QUANTITY") %>'></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblQuantity" runat="server" Text='<%# Bind("QUANTITY") %>'></asp:Label> 
           </ItemTemplate>
       </asp:TemplateField>

        <asp:TemplateField HeaderText="LicType" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtUnit" style="width: 100px;" runat="server" AutoPostBack="false" Text='<%# Bind("UNIT") %>'></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblUnit" runat="server" Text='<%# Bind("UNIT") %>'></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="UnitList" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtUnitListPrice" style="width: 80px;" runat="server" autopostback="false" Text='<%# Bind("UNITLISTPRICE") %>'></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblUnitListPrice" runat="server" Text='<%# Bind("UNITLISTPRICE") %>'></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="ExtList" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtExtendedListPrice" style="width: 80px;" runat="server" autopostback="false" Text='<%# Bind("EXTENDEDLISTPRICE") %>'></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblExtendedListPrice" runat="server" Text='<%# Bind("EXTENDEDLISTPRICE") %>'></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="AcadDisc" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtExtendedAcademicDiscAmt" style="width: 50px;" runat="server" ReadOnly="true" autopostback="false" Text='<%# Bind("EXTENDEDACADEMICDISCAMT") %>'></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblExtendedAcademicDiscAmt" runat="server" Text='<%# Bind("EXTENDEDACADEMICDISCAMT") %>'></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="%AddlDisc" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtApproxDiscretionDiscPct" style="width: 30px;" runat="server" readonly="true" autopostback="false" Text='<%# Bind("APPROXDISCRETIONDISCPCT") %>'></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblApproxDiscretionDiscPct" runat="server" Text='<%# Bind("APPROXDISCRETIONDISCPCT") %>'></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="AddlDisc" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtExtendedDiscretionDiscAmt"  style="width: 50px;" runat="server" autopostback="false" Text='<%# Bind("EXTENDEDDISCRETIONDISCAMT") %>'></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblExtendedDiscretionDiscAmt" runat="server" Text='<%# Bind("EXTENDEDDISCRETIONDISCAMT") %>'></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="UnitNet" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate> 
            <asp:TextBox ID="txtUnitNetPrice"  style="width: 70px;" runat="server" ReadOnly="true" Text='<%# Bind("UNITNETPRICE") %>'></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="Label1" runat="server" Text='<%# Bind("UNITNETPRICE") %>'></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="ExtNet" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate>  
            <asp:TextBox ID="txtExtendedNetPrice" style="width: 70px;" runat="server" ReadOnly="true" Text='<%# Bind("EXTENDEDNETPRICE") %>'></asp:TextBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="lblExtendedNetPrice" runat="server" Text='<%# Bind("EXTENDEDNETPRICE") %>'></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

   </Columns>

Note This works if I create the field with a label as the itemTemplate, though I need to be able to select multiple checkboxes without switching to edit-mode:

       <asp:TemplateField HeaderText="Line test" HeaderStyle-HorizontalAlign="Left"> 
           <EditItemTemplate> 
            <asp:CheckBox ID="TextBox2" style="width: 25px;" runat="server" AutoPostBack="false"></asp:CheckBox> 
           </EditItemTemplate> 
           <ItemTemplate> 
            <asp:Label ID="Label2" style="width: 25px;" runat="server" Text="test"></asp:Label> 
           </ItemTemplate>
        </asp:TemplateField>

Any ideas? Thanks in advance!

Futureproof
  • 375
  • 4
  • 21
  • why do you need a checkbox in the non-editing mode? – ADyson Jan 08 '18 at 14:46
  • @ADyson: why not? – Tim Schmelter Jan 08 '18 at 14:51
  • @TimSchmelter seemed like mixing the read-only and edit modes. But on reflection I suppose it could be selecting rows to delete or something. Fair point. – ADyson Jan 08 '18 at 14:53
  • Show the complete gridview-code. Do you use `AutoGenerateEditButton=true`? Do you handle the `RowEditing`-event`? – Tim Schmelter Jan 08 '18 at 15:07
  • in the markup you posted there is no TextBox named "txtQuantity" – arik Jan 08 '18 at 15:31
  • I don't get it... If you want to use FindControl in a non edit template just leave out the `if (e.Row.RowState == DataControlRowState.Edit) {`? – VDWWD Jan 08 '18 at 15:37
  • @TimSchmelter so I've added the full gridview-code. I'm not auto-generating the edit button. Still, in both RowDataBound and RowEditing events, none of the controls can be found. I need the checkboxes to be available outside of edit-mode so the user can delete or process in bulk. – Futureproof Jan 08 '18 at 20:35

0 Answers0