简体   繁体   中英

RowDataBound Object Not Set to an Instance of an Object

I have a gridview that works fine, but gives me the error "Object Not Set to An Instance of An Object when Edit Clicked.

I believed is because my labels in my gridview are null when in edit mode and this is what causes the problem , but I have no idea how to solve this.

              <asp:BoundField DataField="Received" HeaderText="Received" SortExpression="Received"
                        ReadOnly="true">
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="Complete" SortExpression="Complete">                            
                        <ItemTemplate>
                            <asp:Label ID="lblComplete" runat="server" Text='<%# Bind("Complete") %>'></asp:Label>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>
                    <asp:BoundField DataField="TransTime" HeaderText="Trans. Time" SortExpression="TransTime"
                        ReadOnly="true">
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <asp:LinkButton ID="lbClose" runat="server" CausesValidation="False" CommandName="CloseClicked" Text ="Close"
                                OnClick="CloseClick_Click">Close</asp:LinkButton>
                            <asp:LinkButton ID="lbEdit" runat="server" CausesValidation="False" CommandName="EditRow"  Text =""
                                OnClick="Edit_Click" CommandArgument='<%# Eval("TicketId")%>'>Edit</asp:LinkButton>
                            <asp:LinkButton ID="lbDelete" runat="server" CausesValidation="False" CommandName="DeleteRow"  Text =""
                                OnClick="Delete_Click">Delete </asp:LinkButton>


                        </ItemTemplate>
                        <EditItemTemplate>
                            <asp:LinkButton ID="lbUpdate" runat="server" CausesValidation="True" CommandName="UpdateRow"
                                ForeColor="White" Text="Update" CommandArgument='<%# Eval("TicketId")%>'></asp:LinkButton>
                            <asp:LinkButton ID="lbCancel" runat="server" CausesValidation="False" CommandName="CancelUpdate"
                                ForeColor="White" CommandArgument='<%# Eval("TicketId")%>' Text="Cancel"></asp:LinkButton>
                        </EditItemTemplate>
                        <FooterStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>
                </Columns>
                <EditRowStyle BackColor="#999999" />
                 />

            </asp:GridView>

RowCommand

    protected void gvData_RowCommand(object sender, GridViewCommandEventArgs e)
    {
           if (e.CommandName == "EditRow")
           {
               //This enables the EditTemplate
               int rowindex = ((GridViewRow)             ((LinkButton)e.CommandSource).NamingContainer).RowIndex;
                gvData.EditIndex = rowindex; //Enables the edit row in gridview                      
            }
    }

I get the error in lbClose.Enabled

  protected void gvData_RowDataBound(object sender, GridViewRowEventArgs e)
 {
e.Row.Cells[0].Visible = false; 

if (e.Row.RowType == DataControlRowType.DataRow)
{
    LinkButton lbClose = (LinkButton)e.Row.Cells[5].FindControl("lbClose");
    LinkButton lbEdit = (LinkButton)e.Row.Cells[5].FindControl("lbEdit");
    LinkButton lbDelete = (LinkButton)e.Row.Cells[5].FindControl("lbDelete");


    var lblTrans = (Label)e.Row.FindControl("lblTrans");
    var lblComplete = (Label)e.Row.FindControl("lblComplete");               


    if (e.Row.Cells[3].Text == "")
    {
        lbClose.Enabled = true; //Error Here
        lbEdit.Enabled = true;
        lbDelete.Enabled = true;
    }
    else
    {
        lbClose.Enabled = false;
    }                       
}
}

I believed is because my labels in my gridview are null when in edit mode and this is what causes the problem ...

OK, so you're saying that the problem arises when you enter EditMode on this row. And you're right, those controls don't exist in EditMode because they are part of the ItemTemplate . So just do this:

LinkButton lbClose = (LinkButton)e.Row.Cells[5].FindControl("lbClose");
if (lbClose == null) { return; }

If you can't find the control you know the state of the row and so the statements below don't matter.

Those controls don't exist in EditMode because they are part of the ItemTemplate. Just Change the condition!

if (e.Row.RowType == DataControlRowType.DataRow & gvData.EditIndex != e.Row.RowIndex)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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