2013-08-19 35 views
1

我有工作正常,但給我的錯誤一個GridView「對象未設置爲一個對象時,編輯被點擊的一個實例的實例。的RowDataBound對象未設置爲一個對象

我相信是因爲我的標籤在我的GridView是空在編輯模式下,這是什麼原因造成的問題,但我不知道如何解決這個問題。

   <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      
      } 
    } 

我獲得磅的錯誤Close.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; 
    }      
} 
} 
+0

跟蹤輸出應該告訴你究竟哪一行是問題。我猜想這可能是你的FindControl語句之一返回null。 –

回答

1

我相信是因爲我在我的gridview的標籤是空在編輯模式下,這是什麼原因造成的問題...

好了,你說在此行上輸入EditMode時會出現問題。你說得對,這些控件不存在於EditMode中,因爲它們是ItemTemplate的一部分。因此,只需執行以下操作:

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

如果找不到控件,則知道該行的狀態,因此以下語句無關緊要。

+0

這樣做。謝謝你,忘了使用return – Apollo

+0

你可以分享gridview和sqldatasource完整的aspx頁面! –

0

這些控件在EditMode中不存在,因爲它們是ItemTemplate的一部分。只需更改條件!

if (e.Row.RowType == DataControlRowType.DataRow & gvData.EditIndex != e.Row.RowIndex) 
相關問題