动态添加时用户控件无法正常工作 [英] User control not working when added dynamically
问题描述
  1。我只是在asp.net中创建了一个用户控件,它有一个文本框,日历和一个按钮。
2。在该按钮的点击事件中,我正在显示日历,并且在日历的"onselectionchanged"事件中,我将所选日期传递到文本框。
3。现在我有一个.aspx页面,我在RUN TIME添加了这个用户控件。
4。添加了用户控件,但是我使日历可见的按钮的单击事件不会被触发。
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.
推荐答案
马修,
因为您要在每个Page_Load上添加usercontrol,所以不会触发usercontrol的事件。解决这个问题的第一步是检查请求是否是回发( Page.IsPostBack )。
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!
希望这有帮助
编辑:
哦,顺便说一句:
- 我会将ScriptManager放在页面上在UserControl上。
- ScriptMananager应该是页面上的第一个控件
这篇关于动态添加时用户控件无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!