FindControl Doesn't work if called from another class

In page_Init

I create a number UpdatePanel

and inside these UpdatePanel

one Panel

in each. Then I use this panel to further advance and add other controls dynamically. For example, I add a number TextBoxe

and Button

in each of these panels. Also, I bind the click event to all buttons that are dynamically created. AddressOf

these click events are in another class called Events. In the Sub from the Event class, when I try to find the control, it doesn't seem to work.

Here is the code in the EVENT class

Public Class Events
Inherits System.Web.UI.Page

    Public Sub Dynamic_Btn_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim SQL As New SQLControl

        Dim sTempPanel1 As UpdatePanel = FindControl("MyUpdatePanel1")
        MsgBox("MyUpdatePanel1" & ":" & (sTempPanel1 Is Nothing))

    End Sub
End Class

      

NOTE. If the same subcomponent above is copied / pasted into the code behind the page class, it works.

I know some link is missing here. Maybe FindControl

not referring to the page to search.

+3


source to share


1 answer


It's understandable why this won't work - you are using FindControl

belonging to your class Events

. But buttons are not included in the class Event

. Why you chose this architecture is another matter.

What can you do to get the panel your button sits on call

Dim b as Button = DirectCast(sender, Button)
Dim p as UpdatePanel = = DirectCast(b.Parent, UpdatePanel)

      

Also remember the following: the [FindControl] method only searches for the immediate or top-level page of the container; it does not recursively look for controls in the naming containers contained in the page.

But if you don't really know the exact location of the control, you can write a recursive function to find it



If you know what you have

-- Page
   -- UpdatePanel
      -- Panel
         -- Button

      

You can choose hard

button.Parent.Parent ' <-- this is your update panel

      

0


source







All Articles