none
User control not working when added dynamically RRS feed

  • Question

  •  1. I have simply created a user control in asp.net which has a textbox,calendar and a button.
     2. On the click event of that button i am making the calendar visible,and on the calendar's `onselectionchanged` event i am passing the selected date to the textbox.
     3. Now i have a .aspx page in which i am adding this user control at RUN TIME.
     4. The user control gets added,but the click event of the button by which i am making the calendar visible is not getting fired.

    What is the Issue? Its working fine when i add that user control at design time.
    But when i add it at run time in not working.

    <!-- START OF USER CONTROL CODE(FULL CODE OF USER CONTROL) -->

        <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Dates.ascx.cs" Inherits="Date"%> 
            <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
                <ContentTemplate> 
                   <asp:TextBox ID="TextBox1" onblur="upperCase()" runat="server"></asp:TextBox> 
                   <asp:Button ID="Button9" runat="server" onclick="Button9_Click" Text="..." /> 
                   <asp:Calendar ID="Calendar1" runat="server" onselectionchanged="Calendar1_SelectionChanged1" Visible="False" /></asp:Calendar> 
                   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>          
               </ContentTemplate> 
            </asp:UpdatePanel>

    <!-- END OF USER CONTROL CODE -->

    <!-- START OF CODE WHERE I AM IMPLEMENTING THE USER CONTROL -->

    //created a public object named 'users' of control class
    public partial class View_now : System.Web.UI.Page
    {
     public Control users; 
    }
    
    
    //loaded the user control in page load event
    protected void Page_Load(object sender, EventArgs e)
    {  
            users = LoadControl("~\\Dates.ascx");
    }
    
    
    //applied the user control to a panel
    protected void Button2_Click(object sender, EventArgs e)
    {
            Panel2.Controls.Add(users);
    }

    <!-- END OF CODE WHERE I AM IMPLEMENTING THE USER CONTROL -->

    Now when i click the user control button.It doesn't work.Please help.

    • Moved by Mike Dos Zhang Wednesday, May 30, 2012 7:40 AM (From:Visual C# Language)
    Tuesday, May 29, 2012 12:45 PM

All replies

  • Hi Mathew,

    Because you are adding your usercontrol on each Page_Load, the events of the usercontrol aren't being fired. First step to fix this is check to see if the request is a postback (Page.IsPostBack).

    Your issue has been addressed multiple times, good luck!

    Hope this helps

    EDIT:

    Oh, and by the way:

    • I would place the ScriptManager on the page in stead of on the UserControl.
    • a ScriptMananager should be the first control on the page

    Mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick


    Tuesday, May 29, 2012 1:02 PM
  • Hi ,

     This is a question about asp.net , You may have more luck getting answers in the ASP forums rather than the C# forums.

     Have a good day!


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, May 30, 2012 6:46 AM