1

I have a web page (that uses a master page and 2 Content sections) with an initial user control on it. I placed a dynamically created user control into that initial user control in a certain place(within a PlaceHolder) on that initial user control.

After building the user control dynamically in its Page_Init event, in the dynamically created user controls Page_PreRender event, I am trying to get reference to a label control called strExistingAssignementsLabel using a FindControl method so I can set its ".text" but it does not find it.

I check for "Is Nothing" and that's what I get.

When I look at the page source(not showing all here.) I see that the ID I am looking for is there. It is strExistingAssignementsLabel. As shown below:

<div id="ExistingAssignmentsBreakout"> 
    <table style="margin-left: 30px;"><tr><td>Existing Assignments:</td><td><asp:Label
     runat="server" ID=strExistingAssignementsLabel style="font-family: 'Trebuchet MS','Tahoma';
      font-size: 18px;"></asp:Label></td></tr><tr><td>&nbsp;</td><td><table><tr><td>IP:</td><td>
     <asp:Label runat="server" ID=strIPAddressLabelID style="font-family:

I do see that there is no placeholder - phAssignment in the source. Should it be there?

Any ideas on how to resolve this?

For simplicity...here is the web page with the initial user control in it.

 <%@ Page Title="Account Preferences - Control Panel" Language="VB" 
  MasterPageFile="Layout.master" AutoEventWireup="false" CodeFile="createipjustification.aspx.vb" 
  Inherits="v2_preferences" ValidateRequest="false" %>
  <%@ Register TagPrefix="RSNET" TagName="IPJustificationAdd"
  Src="~/UserControls/User/IPJustification-Add.ascx" %>

 <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="content" Runat="Server">
 <div id="IpJustification">
    <RSNET:IpJustificationAdd ID="ipjaIPJustificationAdd" runat="server" />
 </div>
 </asp:Content>

Here's the the dynamically created user control I placed within that initial user control. (I build the user control HTML in the code-behind - int the Page_Init event.):

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ExistingAssignmentsBreakout.ascx.vb"
Inherits="UserControls_User_ExistingAssignmentsBreakout" %>

<div id="ExistingAssignmentsBreakout"> 
  <asp:PlaceHolder ID="phAssignment" runat="server"></asp:PlaceHolder>
</div>

Here's the initial user control that I placed the dynamically created user control into. I have the dynamically created user control inserted where I want it. It's within a PlaceHolder.

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="IPJustification-Add.ascx.vb"
Inherits="UserControls_User_IPJustification_Add" %>
<%@ Register TagPrefix="NET" TagName="ExistingAssignments"
Src="~/UserControls/User/ExistingAssignmentsBreakout.ascx" %>

<div style="width: 600px; display: block;>
   <table style="margin-left: 30px;">
       <tr>
           <td>Customer:</td>
           <td style="width: 420px;">
               <asp:Label ID="lblCustomerName" runat="server" style="font-family: 'Trebuchet MS'>
           </td>
       </tr>
       <asp:PlaceHolder ID="iaphExistingAssignments" runat="server">
          <NET:ExistingAssignments ID="ExistingAssignments" runat="server" />
       </asp:PlaceHolder>
       <tr>
           <td>Requested Subnet Size:</td>
           <td>
               <asp:DropDownList runat="server" id="ddlRequestedSubnetSize" style="font-family:
               'Trebuchet MS'>
               </asp:DropDownList>
           </td>
       </tr>
   </table>
</div>

In the Page_Init (scaled down...):

    Dim litHtml As Literal

    Dim labelHtml As Label
    Dim labelExistingAssignments As Label
    Dim labelIP As Label

    Dim strExistingAssignementsLabelID As String
    Dim strIPAddressLabelID As String

    ' Start building the HTML.
    litHtml = New Literal
    litHtml.Text = "<table style=""margin-left: 30px;"">"
    phAssignment.Controls.Add(litHtml)

    litHtml = New Literal
    litHtml.Text = "<tr><td>Existing Assignments:</td><td><asp:Label runat=""server"" 
    ID=""strExistingAssignementsLabel"" style=""font-family: 'Trebuchet MS','Tahoma'; font-size: 
    18px;""></asp:Label></td></tr>"
    phAssignment.Controls.Add(litHtml)

    ' End building the HTML.
    litHtml = New Literal
    litHtml.Text = "</table>"
    phAssignment.Controls.Add(litHtml)

In the Page_PreRender (scaled down...):

    Dim labelExistingAssignments As Label
    Dim strExistingAssignmentsID As String

     ' Find the ID in the page. 
     labelExistingAssignments = CType(phAssignment.FindControl("strExistingAssignementsLabel"), Label) 

     If labelExistingAssignments Is Nothing Then
        lblMessage.Text = "Is Nothing"
     End If

Per the suggestions, I added a function to execute the FindControl. and I call it as such:

    Dim labelExistingAssignments As Label
    labelExistingAssignments = FindChildControl(phAssignment, "strExistingAssignementsLabel")

The function:

    Public Function FindChildControl(start As Control, id As String) As Control

       If start IsNot Nothing Then
           Dim foundControl As Control
           fcoundControl = start.FindControl(id)

           If foundControl IsNot Nothing Then
              Return foundControl
           End If

           For Each c As Control In start.Controls
               foundControl = FindChildControl(c, id)
               If foundControl IsNot Nothing Then
                   Return foundControl
               End If
           Next
       End If

       Return Nothing
   End Function
user3020047
  • 868
  • 1
  • 15
  • 45
  • try searching recursively, also make sure there is no `If Not Page.Postback` code on your load or preinit that's causing your controls not to be added on postback. http://stackoverflow.com/questions/5276573/asp-net-findcontrol-recursively – prospector Dec 30 '14 at 04:29
  • You are going to have to do a recursive FindControl on the placeholder to get a reference to the dynamic control – Ubaid Ashraf Dec 30 '14 at 04:33
  • I removed the "If Not IsPostBack" in the page_load in the user control that I am adding the dynamic control to. I also made a function to do a recursive FindControl. Doing these did not change the results. – user3020047 Dec 30 '14 at 14:17

0 Answers0