Results 1 to 2 of 2

Thread: adding server controls programmaticly

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2002
    Posts
    4

    Angry adding server controls programmaticly

    In the page load sub, I am adding a checkbox to a table cell with the following code:

    Dim chkNewCheckbox As New CheckBox()

    tblMyTable.Rows(0).Cells(0).Controls.Add(chkNewCheckBox)

    The control adds and displays OK but when I try to use it in any fashion by employing the findcontrol method, I get a error msg that reads 'Object reference not set to an instance of an object.'

    The code that I'm using to locate the control looks like this:

    Dim chkYourCheckbox As CheckBox = CType(tblMyTable.Rows(0).Cells(0).FindControl("chkNewCheckbox"), CheckBox)

    If (chkYourCheckbox.Checked = True) Then
    Response.Write("Checked")
    Else
    Response.Write("UNChecked")
    End If

    Anybody have experience with this situation?

    In general, I'm having problems getting access to the methods and properties of child controls. What is the .NET methodology for getting such access?

  2. #2
    New Member
    Join Date
    Dec 2002
    Posts
    4
    You need to put an identifier on it.

    Instead do something like

    Dim chkNewCheckbox As New CheckBox()
    'Set the id attribute so that find control can look for something.
    chkNewCheckbox.ID = "chkNewCheckbox"

    Then you can use findcontrol.

    Also, below your class declaration you can do something like:

    Private chkNewCheckbox As New CheckBox

    and you can access this checkbox from any method in your code.


    - Justin_

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width