Dynamically create ajax user control doesn't work
I have simple user control with include ajax control - CalendarExtender. This is code:
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
TagPrefix="cc1"
%><br />
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="true"></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" Format="yyyy-MM-dd"
TargetControlID="TextBox2">
</cc1:CalendarExtender><br /><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
On Page_Load event for this control I have code - just show on label date from calendar:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = TextBox2.Text;
}
First, I try dynamically create this control in Page_Load event for my web form:
protected void Page_Load(object sender, EventArgs e)开发者_如何学C
{
Controls_MySimpleControl datap = LoadControl("~/Controls/MySimpleControl.ascx") as
Controls_MySimpleControl;
Panel1.Controls.Add(datap);
}
Above code doesn't work (user control has been added and page works, but choose date in calendar not appear in label ). But when I above code place in Page_Init event, everything is OK. Could you explain, why it works in Page_Init, and doesn't work in Page_Load. Maybe, I should dynamically create control in different way?
You need only one scriptmanager per page. Your control has it's own script manager for each instance of the user control getting ajax confused.
精彩评论