mathewtinus
mathewtinus

Reputation: 49

ASP.Net User Control not working when added at run time

  1. I have simply created a user control in asp.net which has a textbox,calendar and a button.
    1. 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.
    2. Now i have a .aspx page in which i am adding this user control at RUN TIME.
    3. 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.

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

//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);
}

Now when i click the usercontrol's button,the click event doesn't fire.

Upvotes: 0

Views: 1602

Answers (1)

walther
walther

Reputation: 13600

Add the controls during Page_Init() not Page_Load(). That should do the trick.

Upvotes: 1

Related Questions