Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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."
 
 
 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 13-Mar-12 1:33am
Edited 13-Mar-12 1:34am
v2
Comments
shreekar at 13-Mar-12 11:33am
   
Show the code that is dynamically creating the control.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Are you sure it's not called "FirstName"? Control IDs are case-sensitive.
  Permalink  
Comments
Diler_patel at 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
   
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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

@ Shreekar
 
' 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
 
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 Zoltán Zörgő 139
2 George Jonsson 135
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 14 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100