Link to home
Start Free TrialLog in
Avatar of dbasplus
dbasplusFlag for Australia

asked on

Using FindControl with a ContentPlaceHolder in ASP.NET

I need to create a dynamically expanding entry form and the following example looks to go a long way in helping me do this.
http://www.asp.net/Learn/ajax-videos/video-286.aspx

Unfortunately this does not work well when using MasterPages and the ContentPlaceHolder.

The names returned back from Page.Request.Form are in the form masterpage$pagecontent$controlname

If I strip the name down and so on I get better results but still strange idiosyncrasies.

Is there a better way to achieve the desired result?


Public Shared Function GetPostBackControl(ByVal thePage As Page) As Control
        Dim myControl As Control = Nothing
        Dim ctrlName As String = thePage.Request.Params.Get("__EVENTTARGET")
        If ((ctrlName IsNot Nothing) And (ctrlName <> String.Empty)) Then
            myControl = thePage.FindControl(ctrlName)
        Else
            Dim m As MasterPage = thePage.Master
            Dim mc As ContentPlaceHolder = m.FindControl("maincontent")
            Dim Item As String
            For Each Item In thePage.Request.Form
                ' The name of each item can be returned like this when using a MasterPage: "ctl00$maincontent$controlname" 
                '"$" = thepage.idseperator
                If InStr(Item, m.ID & "$" & mc.ID & "$") > 0 Then
                    Item = Right(Item, Len(Item) - Len(m.ID & "$" & mc.ID & "$"))
                ElseIf InStr(Item, m.ID & "$") > 0 Then
                    Item = Right(Item, Len(Item) - Len(m.ID & "$"))
                End If
 
                Dim c As Control = mc.FindControl(Item)
                If (TypeOf (c) Is System.Web.UI.WebControls.Button) Then
                    myControl = c
                End If
            Next
 
        End If
        Return myControl
    End Function

Open in new window

Avatar of jazz__man
jazz__man

Here is a great article on masterpages that covers all the common quirks and their solutions.

http://odetocode.com/articles/450.aspx

cheers
Avatar of dbasplus

ASKER

This site gave me greater knowledge of how masterpages are called but still did not solve my question.
For now I have given up and will try again later.
ASKER CERTIFIED SOLUTION
Avatar of jazz__man
jazz__man

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Whilst the above was helpful to understand the concept, and should fix some users issues with this, I had to rework the pages to be completely different.
@dbasplus

I don't understand why you feel the need to grade the answer as a C. I put in a fair bit of time going to your suggested website and going through the tutorial. I then managed to get the very example you referred to working in visual studio 2008. I feel I have addressed your problem significantly well and as for this " I had to rework the pages to be completely different." , if it was to be completely different then you should have said so in your question.

So grade C - totally unjustified in my opinion!
Sorry Jazz Man, I just graded it like that as it did not resolve my issue. I really appreciated your work and, as stated, it will no doubt work for someone else; it just didn't work for me.