Click here to Skip to main content
15,891,763 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i Have Created A GridView with Template Fields Dynamically. . The Problem is I Want to Acces those Dynamically Created Controls but when i Say Find Control its Giving NUllEXCEPTIONERROR error "Object reference not set to an instance of an object."

VB
For i = 0 To AuthorsGridView.Rows.Count - 1
           Dim txt As TextBox = AuthorsGridView.Rows(i).Cells(0).FindControl("firstName")
           '' Error Run Time
           txt.Text = ""
           If txt.Text = "SomeValue" Then
               'txt.ForeColor = Drawing.Color.Red
               txt.Font.Bold = True
           Else
               txt.Font.Italic = True
           End If
       Next i


Plz Help me Out
Posted
Updated 13-Mar-12 0:34am
v2
Comments
shreekar 13-Mar-12 11:33am    
Show the code that is dynamically creating the control.

Are you sure it's not called "FirstName"? Control IDs are case-sensitive.
 
Share this answer
 
Comments
Diler Patel 14-Mar-12 7:25am    
is it ? case Sensitive? i m using VB code so i dont think its Case Sensitive
And problem is i can access those controls in same Function but not in other if i click any button also i loses my grid along wit control
#realJSOP 14-Mar-12 7:56am    
VB syntax is (generally) case insensitive, but we're not talking about code, we're talking about the name of an index key ("firstName"). Those are always case-sensitive.
Diler Patel 17-Mar-12 6:23am    
ya i checked but its Giving the Same Problem Actually if post back is done then i loses my dynamically gridview
@ Shreekar

VB
' Here my Code is

Public Class GridViewTemplate
        Implements ITemplate

        Private templateType As DataControlRowType
        Private columnName As String

        Sub New(ByVal type As DataControlRowType, ByVal colname As String)

            templateType = type
            columnName = colname

        End Sub

        Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
          Implements ITemplate.InstantiateIn

            ' Create the content for the different row types.
            Select Case templateType
                Case DataControlRowType.Header
                    ' Create the controls to put in the header 
                    ' section and set their properties. 
                    Dim lc As New Literal
                    lc.Text = " " & UCase(columnName) & ""
                    ' Add the controls to the Controls collection
                    ' of the container. 
                    container.Controls.Add(lc)

                Case DataControlRowType.DataRow
                    ' Create the controls to put in a data row
                    ' section and set their properties.
                    Dim firstName As New TextBox  
                    firstName = New TextBox
                    Dim lastName As New Label
                    firstName.ID = "txtFName"

                    Dim spacer = New Literal
                    spacer.Text = "--"

                    ' To support data binding, register the event-handling methods
                    ' to perform the data binding. Each control needs its own event
                    ' handler.
                    AddHandler firstName.DataBinding, AddressOf FirstName_DataBinding
                    AddHandler lastName.DataBinding, AddressOf LastName_DataBinding
                    lastName.Visible = False
                    ' Add the controls to the Controls collection
                    ' of the container.
                    container.Controls.Add(firstName)
                    container.Controls.Add(spacer)
                    container.Controls.Add(lastName)

                    ' Insert cases to create the content for the other 
                    ' row types, if desired.

                Case Else

                    ' Insert code to handle unexpected values. 

            End Select

        End Sub

        Private Sub FirstName_DataBinding(ByVal sender As Object, ByVal e As EventArgs)

            ' Get the Label control to bind the value. The Label control
            ' is contained in the object that raised the DataBinding 
            ' event (the sender parameter).
            Dim l As TextBox = CType(sender, TextBox)

            ' Get the GridViewRow object that contains the Label control. 
            Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)

            ' Get the field value from the GridViewRow object and 
            ' assign it to the Text property of the Label control.
            l.Text = DataBinder.Eval(row.DataItem, "Doctor_Name").ToString()

        End Sub

        Private Sub LastName_DataBinding(ByVal sender As Object, ByVal e As EventArgs)

            ' Get the Label control to bind the value. The Label control
            ' is contained in the object that raised the DataBinding 
            ' event (the sender parameter).
            Dim l As Label = CType(sender, Label)

            ' Get the GridViewRow object that contains the Label control.
            Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)

            ' Get the field value from the GridViewRow object and 
            ' assign it to the Text property of the Label control.
            l.Text = DataBinder.Eval(row.DataItem, "SlNo").ToString()

        End Sub

    End Class

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Label1.Text = "Entered"
        Dim dt As DataTable
        Dim dr As DataRow
        dt = New DataTable()
        dt.Columns.Add(New DataColumn("Doctor_Name"))
        dt.Columns.Add(New DataColumn("SlNo"))
        dr = dt.NewRow()
        dr("Doctor_Name") = "Val1"
        dr("Slno") = "1001"
        dt.Rows.Add(dr)
        Label1.Text = "1 add"
        dr = dt.NewRow()
        dr("Doctor_Name") = "Val2"
        dr("Slno") = "1002"
        dt.Rows.Add(dr)
        Label1.Text = "2 add"
        dr = dt.NewRow()
        dr("Doctor_Name") = "Val3"
        dr("Slno") = "1003"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("Doctor_Name") = "val4"
        dr("Slno") = "1004"
        dt.Rows.Add(dr)
        Label1.Text = "4 add"

        Dim customField As New TemplateField
        Dim CustomField1 As New TemplateField
        ' Create the dynamic templates and assign them to
        ' the appropriate template property.
        customField.ItemTemplate = New GridViewTemplate(DataControlRowType.DataRow, "Author Name")
        customField.HeaderTemplate = New GridViewTemplate(DataControlRowType.Header, "Employees")
        'CustomField1.ItemTemplate = New GridViewTemplate(DataControlRowType.DataRow, "Author Name 2")
        'CustomField1.HeaderTemplate = New GridViewTemplate(DataControlRowType.Header, "Author Name 2")
        ' Add the field column to the Columns collection of the
        ' GridView control.
        AuthorsGridView.Columns.Add(customField)
        'AuthorsGridView.Columns.Add(CustomField1)
        AuthorsGridView.DataSource = dt
        Label1.Text = " DataSource Set"
        AuthorsGridView.DataBind()
        ViewState("table") = dt
        Label1.Text = "Binded"
' Here This Code Works it Shows Me the Value I Can Access it But Out side this Fun i Cant Access all Controls just Goes of
        For i = 0 To AuthorsGridView.Rows.Count - 1
            Dim txt As TextBox = AuthorsGridView.Rows(i).Cells(0).FindControl("txtFName")
            '' Error Run Time
            'txt.Text = ""
            Label1.Text = txt.Text
            If txt.Text = "VAl2" Then
                'txt.ForeColor = Drawing.Color.Red
                txt.Font.Bold = True
            Else
                txt.Font.Italic = True
            End If

        Next
    End Sub

' Here on Other Control  click i want to Access it But its Not Happening
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click


        Dim dt As DataTable
        dt = ViewState("table")
        'AuthorsGridView.Columns.Add(CustomField1)
        AuthorsGridView.DataSource = dt
        'Label1.Text = " DataSource Set"
        AuthorsGridView.DataBind()
        Label1.Text = AuthorsGridView.Rows.Count
        For i = 0 To AuthorsGridView.Rows.Count - 1
            Dim txt As TextBox = AuthorsGridView.Rows(i).Cells(0).FindControl("txtFName")
            
            Label1.Text = txt.Text
            If txt.Text = "Val2" Then
                
                txt.Font.Bold = True

            Else
                txt.Font.Italic = True
            End If

        Next
    End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900