本文介绍了如何访问动态创建的控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经动态创建了一个带有模板字段的GridView. .问题是我想访问那些动态创建的控件,但是当我说找到控件时,就会给它NUllEXCEPTIONERROR错误对象引用未设置为对象的实例."

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

推荐答案


' 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


这篇关于如何访问动态创建的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 19:47