Event not firing WITHIN a dynamically created UserControl placed with a PlaceHolder control. RRS feed

  • Question

  • User876221121 posted

    I am having an issue with an event not firing within a UserControl, only when it is dynamically created and added to a Placeholder control. When I simply have a static instance of the control on a web page, the event fires fine. 

    Here is the flow of what is happening:

    1) User clicks on image to add a new dynamic user control to the placeholder

    2) User control is created and added to the placeholder, and to a page list so that it will be recreated on postback.  This list is also persisted to the session, so that it is available on postback.

    3) User clicks the dropdown within the newly created user control, which causes a post back.

    4) On postback, the controls are taken out of the session and recreated on Page_Init

    5) The OnSelectedIndexChanged event within the user control never fires, so the Texbox text within the control is never set.  I verified that the index did change when I recreated the user controls on post back, by checking the selectedindex value.)

    Code is below:

    Page that hosts the PlaceHolder

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ServiceLevelAgreement.aspx.cs" Inherits="ServiceLevelAgreement" Title="Untitled Page" %>

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p><%@ Register TagPrefix="uc" TagName="ConsumerSelection" Src="~/ConsumerSelection.ascx" %><o:p></o:p><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"><o:p></o:p> <table><o:p></o:p>      <tr><o:p></o:p>            <td valign="top" rowspan="2"><o:p></o:p>              <asp:PlaceHolder ID="uxConsumerPlaceHolder" runat="server"><o:p></o:p>              </asp:PlaceHolder><o:p></o:p>                  <asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton1_Click" /><o:p></o:p>            </td><o:p></o:p>   


    <o:p></o:p></table></asp:Content><o:p></o:p><o:p> </o:p><o:p></o:p><o:p>Code behind for page above....</o:p><o:p> </o:p><o:p></o:p>public partial class ServiceLevelAgreement : System.Web.UI.Page<o:p></o:p>{<o:p></o:p>   
    private List<ConsumerSelection> m_ConsumerSelections = new List<ConsumerSelection>();<o:p></o:p>    protected override void OnInit(EventArgs e)<o:p></o:p>    {<o:p></o:p>       
    if (Session["list"] != null)<o:p></o:p>        {<o:p></o:p>           
    m_ConsumerSelections = (List<ConsumerSelection>)Session["list"];<o:p></o:p>       
    if (m_ConsumerSelections != null)<o:p></o:p>        {<o:p></o:p>           
    foreach (ConsumerSelection selection in m_ConsumerSelections)<o:p></o:p>            {<o:p></o:p>               
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)<o:p></o:p>    {<o:p></o:p>       
    ConsumerSelection selection = (ConsumerSelection)LoadControl("ConsumerSelection.ascx");<o:p></o:p>       
    if ( m_ConsumerSelections != null)<o:p></o:p>        {<o:p></o:p>               
    selection.ID = "selection" + m_ConsumerSelections.Count;<o:p></o:p>       
    selection.ID = "selection0";<o:p></o:p>       
    }<o:p></o:p><o:p> </o:p>       
    Session["list"] = m_ConsumerSelections;<o:p></o:p>        this.uxConsumerPlaceHolder.Controls.Add(selection);<o:p></o:p>   



    Here is the user control markup:

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="ConsumerSelection.ascx.cs" Inherits="ConsumerSelection" %>

    <o:p></o:p><asp:DropDownList ID="DropDownList1" runat="server"><o:p></o:p>        <asp:ListItem Text="1" Value="1"></asp:ListItem><o:p></o:p>   

        <asp:ListItem Text="2" Value="2"></asp:ListItem>

    <o:p></o:p></asp:DropDownList>&nbsp;<o:p></o:p><asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged"><o:p></o:p>        <asp:ListItem Text="1" Value="1"></asp:ListItem><o:p></o:p>   

        <asp:ListItem Text="2" Value="2"></asp:ListItem>

    <o:p></o:p></asp:DropDownList>&nbsp;<o:p></o:p><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><o:p></o:p>&nbsp;<o:p></o:p><asp:DropDownList ID="DropDownList3" runat="server"><o:p></o:p></asp:DropDownList><o:p></o:p><o:p> </o:p><o:p>and </o:p><o:p>the code behind for the user control:</o:p><o:p>    </o:p><o:p>public </o:p><o:p>partial class ConsumerSelection : System.Web.UI.UserControl<o:p></o:p>    </o:p><o:p>{<o:p></o:p>        </o:p>
    <o:p>protected override void OnInit(EventArgs e)<o:p></o:p>        </o:p><o:p>{<o:p></o:p>            </o:p><o:p>base.OnInit(e);</o:p><o:p>}<o:p></o:p>        </o:p>
    <o:p>protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)<o:p></o:p>        </o:p><o:p>{<o:p></o:p>            </o:p>
    <o:p>this.TextBox1.Text = DropDownList2.SelectedItem.Text;<o:p></o:p>        </o:p>
    <o:p><o:p></o:p>}<o:p></o:p> </o:p><o:p>

    Thanks all for any help on this.


    Monday, June 30, 2008 11:07 AM


  • User-1136466523 posted


    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>

    From your description, it seems that you want to fire the event from a user control which added to the page dynamically, right?

    <o:p> </o:p>

    I suggest that you may try the following article which shows a solution on dynamically adding or removing controls, and also some skills on how to handle the events in the corresponding user control.

    <o:p> </o:p>


    <o:p> </o:p>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 3, 2008 2:17 AM