title: How to register user control events when loaded dynamically
body: <p>I have a .aspx page with two drop downs that control which user control is dynamically added to the page using a placeholder. I have two drop downs that the user selected the value on one and loads the other. I have the second drop down value being passed to load the proper user control. Right after the user selects a value from the second drop down the user control shows that it has loaded but when the user clicks the drop down list on the user control the selectedIndexChanged event doesn't fire on the first selection and the value the user selected isn't retained. Once the user selects a value the page seems like it posts back and the value is stored and shows properly in the drop down list on the second selection. Here is my .aspx page that i have simplified to show the two drop downs and the code behind below it. </p>
  
  <pre><code>&lt;%@ Page Title="Add New Property" Language="vb" AutoEventWireup="false"  MasterPageFile="~/Site.Master" CodeBehind="addProperty.aspx.vb" Inherits="project.addProperty" %&gt;
  &lt;%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %&gt;
  &lt;%@ Register Src="~/Clients/lodging.ascx" TagPrefix="uc1" TagName="lodging" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"&gt;
  &lt;style type="text/css"&gt;
      .auto-style1 {
          height: 20px;
      }
   &lt;/style&gt;
  &lt;asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server"&gt;
  &lt;telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"&gt;
      &lt;AjaxSettings&gt;
          &lt;telerik:AjaxSetting AjaxControlID="RadAjaxPanel1"&gt;
              &lt;UpdatedControls&gt;
                  &lt;telerik:AjaxUpdatedControl ControlID="RadAjaxPanel1" LoadingPanelID="RadAjaxLoadingPanel1" UpdatePanelCssClass="" /&gt;
              &lt;/UpdatedControls&gt;
          &lt;/telerik:AjaxSetting&gt;
      &lt;/AjaxSettings&gt;
  &lt;/telerik:RadAjaxManager&gt;
  
  &lt;telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" Width="100%"&gt;
  &lt;table style="width:100%;"&gt;
     &lt;tr&gt;
          &lt;td class="auto-style1" style="text-align: right"&gt;
              Property Type&lt;/td&gt;
          &lt;td&gt;
              &lt;telerik:RadDropDownList ID="dd_ID_Const_PropertyType" runat="server" AppendDataBoundItems="True"  Skin="WebBlue" DataSourceID="sql_getPropertyType" DataTextField="PropertyType" DataValueField="ID_Const_PropertyType" AutoPostBack="True" RenderMode="Auto" CausesValidation="False"&gt;
                 &lt;Items &gt;
                      &lt;telerik:DropDownListItem Text="Select Property Type..." Value=0 /&gt;
                  &lt;/Items&gt;
              &lt;/telerik:RadDropDownList&gt;
          &lt;/td&gt;
          &lt;td class="auto-style4" style="text-align: right"&gt;Property Subtype&lt;/td&gt;
          &lt;td&gt;
              &lt;telerik:RadComboBox ID="dd_ID_Const_PropertySubType" Runat="server" AppendDataBoundItems="True" AutoPostBack="true"
                  Skin="WebBlue" DropDownAutoWidth="Enabled" MaxHeight="250px" Width="300px"
                  RenderMode="Auto" CausesValidation="False" &gt;
                  &lt;Items&gt;
                      &lt;telerik:RadComboBoxItem Text="First Select Property Type..." Value=0 /&gt;
                  &lt;/Items&gt;
              &lt;/telerik:RadComboBox&gt;
          &lt;/td&gt;
      &lt;/tr&gt;&lt;tr&gt;
              &lt;td colspan="4"&gt;
                  &lt;asp:PlaceHolder ID="ph_propertiesSubType" runat="server" EnableViewState="false"&gt;&lt;/asp:PlaceHolder&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;tr&gt;
  &lt;/table&gt;
  &lt;/telerik:RadAjaxPanel&gt;
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>here is my code behind for the aspx page. It is much more complex than this but i tried to make it easy to understand what I was doing. This code is when the user selects a value from the second drop down on the parent page (didn't include the selected index change code for the first drop down because that is working correctly)</p>
  
  <pre><code>     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  
      If IsPostBack Then
          Select Case ("Session")propertySubType
              Case 1
                  ph_propertiesSubType.Controls.Add(lodgingControl)
              Case Else
                  ph_propertiesSubType.Controls.Clear()
          End Select
      End If   
  End Sub
  
       Private Sub dd_ID_Const_PropertySubType_SelectedIndexChanged(sender As Object, e   As RadComboBoxSelectedIndexChangedEventArgs) Handles dd_ID_Const_PropertySubType.SelectedIndexChanged
  
      ("Session")propertySubType = 1
  
  
      'add property subtype indexes to reference
      Select Case ("Session")propertySubType
          Case 1
              ph_propertiesSubType.Controls.Add(lodgingControl)           
          Case Else
              ph_propertiesSubType.Controls.Clear()
      End Select
  
  End Sub
  </code></pre>
  
  <p>once the user selects the proper value the user control is dynamically added to the page. Here is my user control code.</p>
  
  <pre><code>&lt;%@ Control Language="vb" AutoEventWireup="false" CodeBehind="lodging.ascx.vb" Inherits="project.lodging" %&gt;
  &lt;%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %&gt;
  
  &lt;style type="text/css"&gt;
      .auto-style1 {
          height: 25px;
          text-align: right;
      }
      &lt;/style&gt;
  
  &lt;table&gt;
  &lt;tr&gt;
              &lt;td class="auto-style1"&gt;Interior or Exterior Room Entrance&lt;/td&gt;
              &lt;td&gt;
                  &lt;telerik:RadDropDownList ID="dd_ID_Const_RoomEntrance" runat="server" AutoPostBack="true" 
                      AppendDataBoundItems="True" CausesValidation="False" Skin="WebBlue" 
                      SelectedText="Select Market Type..." SelectedValue="0"&gt;
                      &lt;Items&gt;
                          &lt;telerik:DropDownListItem Text="Select Market Type..." Value="0" Selected="True" /&gt;
                      &lt;/Items&gt;
                  &lt;/telerik:RadDropDownList&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
  &lt;/table&gt;
  &lt;asp:SqlDataSource ID="sql_roomEntrance" runat="server" 
          SelectCommand="SELECT ID_Const_RoomEntrance, roomEntrance, Active
  FROM Const_RoomEntrance
  WHERE Active = 'True'
  ORDER BY roomEntrance ASC"&gt;&lt;/asp:SqlDataSource&gt;
  </code></pre>
  
  <p>And the code behind for the user control. I init my database values and they load correctly. The problem i am having is that when the user goes to the drop down list right after the user control loads on the page the on selected index event doesn't trigger the user control drop down list value the first time. Its like the events haven't been registered and I am wondering how to register them. or how to raise the event that might not have been loaded completely. I have my auto post backs set its like my page structure loads but the code doesn't come in with it until another postback. When i set my breakpoints on the selectedIndexChange event they don't get fired on first selection, which I would think means they aren't available or loaded initially, but the breakpoint does get hit on the second selection. </p>
  
  <pre><code> Imports System.Data.SqlClient
      Imports System.Data.Sql
      Imports Microsoft.VisualBasic
      Imports System.Data
      Imports System
      Imports System.Data.OleDb
      Imports System.Collections.Generic
      Imports System.Text
      Imports System.IO
      Imports System.Web
  
      Imports Telerik
      Imports Telerik.Web
      Imports Telerik.Web.UI
  
  Public Class lodging
      Inherits System.Web.UI.UserControl
      Dim querystring As String = ""
      Dim tempTable As New DataTable
      Dim currentDate As String = DateTime.Now.ToString("yyyy-MM-dd")
  
  Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
          dd_ID_Const_RoomEntrance.DataSource = sql_roomEntrance
          dd_ID_Const_RoomEntrance.DataTextField = "roomEntrance"
          dd_ID_Const_RoomEntrance.DataValueField = "ID_Const_RoomEntrance"
          dd_ID_Const_RoomEntrance.DataBind()
  End Sub
  
  Public Sub dd_ID_Const_RoomEntrance_SelectedIndexChanged(sender As Object, e As DropDownListEventArgs) Handles dd_ID_Const_RoomEntrance.SelectedIndexChanged
  
          If dd_ID_Const_RoomEntrance.SelectedValue &lt;&gt; 0 Then
              Session("lodgingRoomEntrance") = dd_ID_Const_RoomEntrance.SelectedValue
          Else
              Session("LodgingRoomEntrance") = 0
          End If
  
      End Sub
  </code></pre>