2012-05-27 69 views
0
  1. 我只是在asp.net中创建了一个具有文本框,日历和按钮的用户控件。
    1. 在该按钮的单击事件上,我正在使日历可见,并且在日历的onselectionchanged事件中,我将所选日期传递给文本框。
    2. 现在我有一个.aspx页面,我在RUN TIME添加这个用户控件。
    3. 用户控件被添加,但我使日历可见的按钮的单击事件没有被触发。

问题是什么?当我在设计时添加用户控件时,它的工作正常。 但是,当我在运行时添加它不工作。ASP.Net用户控件在运行时添加时不起作用

<%@控制语言= “C#” AutoEventWireup = “真” 的CodeFile = “Dates.ascx.cs” 继承= “日期” %>

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

现在,当我点击usercontrol的按钮,点击事件不会触发。

+0

你有没有试过我的建议?使用Page_Init()事件? – walther

+0

是的,我做了,但它没有奏效。 :( – mathewtinus

回答

1

在Page_Init()而不是Page_Load()中添加控件。这应该够了吧。

+1

@mathewtinus,你可以用你想要加载控件的代码更新你的问题吗? – walther

相关问题