1

i have this code:

Dim txt = CType(GridView1.FindControl("cnt_content"), TextBox)
        txt.Attributes.Add("style", "word-wrap:break-word;")

i'm always getting that txt is an object not set to an instance of an object that's my asp code:

 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
    AutoGenerateColumns="False" 
    DataKeyNames="cnt_id">
    <Columns>
<asp:TemplateField HeaderText="Content">
                <EditItemTemplate>
                         <asp:TextBox ID="cnt_content" runat="server" Text='<%# Bind("cnt_content") %>' />
                </EditItemTemplate> 
                <ItemTemplate> 
                    <asp:Label ID="lblcnt_content" runat="server" Text='<%# Bind("cnt_content") %>'></asp:Label> 
                </ItemTemplate> 
                <ItemStyle wrap="true" Width="400px" />
            </asp:TemplateField>  

any help?

6
  • Most likely, I would guess that your control cnt_content does not exist or isn't a TextBox... Commented Sep 4, 2013 at 13:43
  • well it exist and it's a textbox Commented Sep 4, 2013 at 13:44
  • 1
    Well, it doesn't exist where you think it does. Extract the call to GridView1.FindControl("cnt_content") to a local variable and examine it in the debugger - my guess is it will be returning null. Commented Sep 4, 2013 at 13:45
  • Where is the Dim txt = ... code running at? Also, you have 2 controls with the same name. Although allowed in this context that is usually a very bad idea. Commented Sep 4, 2013 at 13:52
  • 1
    It is not a good idea to name controls the same thing, you have a text box and label with the same name of cnt_content. I realize they are in different templates and should never exist at the same time, but it is confusing for anyone else looking at your code. Commented Sep 4, 2013 at 13:53

2 Answers 2

2

My guess is that you are trying to find this control in the RowDataBound event, like this:

Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

End Sub

You need to only check for this control on data rows, not header or footer rows, as the control will not exist in those other types of rows, try this:

Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    ' Only check in data rows, ignore header and footer rows
    If e.Row.RowType = DataControlRowType.DataRow Then
        ' Determine if you are in edit mode or not
        If GridView1.EditIndex = -1 Then
            ' Not in edit mode so look for label control defined in ItemTemplate of grid view
            ' Put logic here for label control
        Else
            ' In edit mode so look for textbox control defined in EditItemTemplate of grid view
            Dim txt = CType(GridView1.FindControl("cnt_content"), TextBox)
            txt.Attributes.Add("style", "word-wrap:break-word;")
        End If
    End If
End Sub
Sign up to request clarification or add additional context in comments.

3 Comments

Updated answer to account for which template (ItemTemplate or EditItemTemplate) of the grid view you are using.
@JocelyneElKhoury - have you tried the code that branches based on whether you are in edit mode or not?
Glad you accepted the answer, feel free to up-vote the answer as well. :-)
0

It means that GridView1.FindControl("cnt_content") is returning null, which likely means that GridView does not directly contain a control called "cnt_content".

1 Comment

it's a textbox in a row in the grid

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.