Valerie Meunier

Valerie Meunier

  • 1.6k
  • 533
  • 26.1k

trying to get text of a label in itemTemplate of a gridview

Apr 16 2021 12:52 PM
Hi
I'm trying to get the text of a label located in a ItemTemplate of a gridview.
 
The error is: Object reference not set to an instance of an object
 
Thanks for help.
V
 
Here the code:
  1. protected void Button2_Click(object sender, System.EventArgs e)    
  2. {    
  3. Label nal;    
  4. foreach (GridViewRow row in GridView1.Rows)    
  5. {    
  6. nal = (Label)GridView1.FindControl("naaml");    
  7. }    
  8. ...    
  9. a = nal.Text; //(Object reference not set to an instance of an object) 
Here the aspx-code:
  1. <asp:GridView ID="GridView1" runat="server" ... />  
  2. <asp:TemplateField >  
  3. <EditItemTemplate>  
  4. <asp:TextBox ID="naam" runat="server" Text='<%# Bind("naam") %>' ></asp:TextBox>  
  5. </EditItemTemplate>  
  6. <ItemTemplate>  
  7. <asp:Label ID="naaml" runat="server" Text='<%# Bind("naam") %>'></asp:Label>  
  8. </ItemTemplate>  
  9. </asp:TemplateField>  
  10. ...  
  11. </asp:GridView>  

Answers (4)