3

On page_Init, I am creating number of UpdatePanels and inside these UpdatePanels one Panel in each. I then use this panel to further go ahead and add other controls dynamically. For example I add number of TextBoxes and Buttons in each of these Panels. Further, I am binding a click event to all the buttons that are created dynamically. The AddressOf these click events are in another class called Events. In the Sub from the Event class, when I try to find a control, It does not 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 above sub is copy / pasted to the code behind of the page class, it works.

I know I am missing some link here. May be the FindControl is not being referenced to the page in which is should search in.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Manish Rajput
  • 43
  • 1
  • 4
  • 2
    `FindControl` in your code is the method of an instance of `Events` page. This instance is not going to contain controls that were added to another page. – Igor Dec 16 '14 at 17:41
  • Thanks for the swift response Igor, I will dig dipper on this and update this thread. I get your point. – Manish Rajput Dec 17 '14 at 03:10

1 Answers1

0

This is clear why it doesn't work - you are running FindControl that belong to your Events class. But the buttons are not on on Event class. Why you chose such architecture - this is different question.

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

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

Also remember this: The method [FindControl] searches only the page's immediate, or top-level, container; it does not recursively search for controls in naming containers contained on the page.

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

If you know that you have

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

You can hardcode

button.Parent.Parent ' <-- this is your update panel
T.S.
  • 18,195
  • 11
  • 58
  • 78
  • I have added the procedures to the same code behind page, i.e. to the same class where I am generating the buttons Dynamically from. However that said, its just making it work. The very reason why I wanted to create the click events from another class was simply to keep the code behind of the main page / class clean, else it was looking too cluttered with code lines. I am just a new bee and learning as I code. Is there a better way to design the whole architecture otherwise? – Manish Rajput Dec 17 '14 at 03:26
  • In fact I would love to tell you what I am trying to achieve and someone could help me with a suggestion on how the architecture should be like. Currently the way I look at what I have designed is too much of coding I guess... :( – Manish Rajput Dec 17 '14 at 03:37
  • @ManishRajput There are many ways to achieve same goals. This this why there are patterns and anti-patterns. Which means, programmers developed both, *good* and *not so good* common ways to solve similar issues. When it comes to plain ASP.net, good pattern to start is MVP [Model-View-Presenter]. Check this out. http://stackoverflow.com/questions/2056/what-are-mvp-and-mvc-and-what-is-the-difference Looks like your question is answered here. We know why you can't use `FindControl` in the instance of a class which is not parent to control, and you also now at this time how to reach it. The End. – T.S. Dec 17 '14 at 14:39