J
JacekDr
Hello,
I've got the following problem:
I want to add and remove dynamically controls to UpdatePanel.
In my user control I have a button, but when I click it I get
AsyncPostback and Event for ButtonClick in UserControl doesn't fire.
<asp:ScriptManager ID="ScriptManager" EnableViewState="true"
runat="server" EnablePageMethods="True" >
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" EnableViewState="true"
runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button Text="+Add Person" ID="AddPerson"
runat="server" OnClick="AddPerson_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button Text="+Save" ID="SavePersons" runat="server"
OnClick="SavePersons_Click" />
Here is my code that adds new user control to UpdatePanel:
protected void AddPerson_Click(object sender, EventArgs e)
{
UserControls_Person ctrl =
(UserControls_Person)LoadControl("UserControls/Person.ascx");
ctrl.ID = "Person" +
UpdatePanel.ContentTemplateContainer.Controls.Count.ToString();
UpdatePanel.ContentTemplateContainer.Controls.Add(ctrl);
HttpContext.Current.Session.Remove("owners");
HttpContext.Current.Session.Add("owners",
UpdatePanel.ContentTemplateContainer.Controls);
UpdatePanel.Update();
}
After postback I create controls in CreateChildControls:
protected override void CreateChildControls()
{
base.CreateChildControls();
if (Page.IsPostBack)
{
if (HttpContext.Current.Session["owners"] != null)
{
for (int i = 0; i <
((ControlCollection)HttpContext.Current.Session["owners"]).Count; i++)
{
if
(((ControlCollection)HttpContext.Current.Session["owners"])
.GetType().BaseType == typeof(UserControls_Person))
{
UpdatePanel.ContentTemplateContainer.Controls.Add(((ControlCollection)HttpContext.Current.Session["owners"])
);
i--;
}
}
}
HttpContext.Current.Session.Remove("owners");
HttpContext.Current.Session.Add("owners",
UpdatePanel.ContentTemplateContainer.Controls);
}
}
In my UserControl in Page_Load method I have:
this.DelOwner.Click += new EventHandler(DelOwner_Click);
Could U help me what did I wrong?
BR,
Jack
I've got the following problem:
I want to add and remove dynamically controls to UpdatePanel.
In my user control I have a button, but when I click it I get
AsyncPostback and Event for ButtonClick in UserControl doesn't fire.
<asp:ScriptManager ID="ScriptManager" EnableViewState="true"
runat="server" EnablePageMethods="True" >
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel" EnableViewState="true"
runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button Text="+Add Person" ID="AddPerson"
runat="server" OnClick="AddPerson_Click" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button Text="+Save" ID="SavePersons" runat="server"
OnClick="SavePersons_Click" />
Here is my code that adds new user control to UpdatePanel:
protected void AddPerson_Click(object sender, EventArgs e)
{
UserControls_Person ctrl =
(UserControls_Person)LoadControl("UserControls/Person.ascx");
ctrl.ID = "Person" +
UpdatePanel.ContentTemplateContainer.Controls.Count.ToString();
UpdatePanel.ContentTemplateContainer.Controls.Add(ctrl);
HttpContext.Current.Session.Remove("owners");
HttpContext.Current.Session.Add("owners",
UpdatePanel.ContentTemplateContainer.Controls);
UpdatePanel.Update();
}
After postback I create controls in CreateChildControls:
protected override void CreateChildControls()
{
base.CreateChildControls();
if (Page.IsPostBack)
{
if (HttpContext.Current.Session["owners"] != null)
{
for (int i = 0; i <
((ControlCollection)HttpContext.Current.Session["owners"]).Count; i++)
{
if
(((ControlCollection)HttpContext.Current.Session["owners"])
.GetType().BaseType == typeof(UserControls_Person))
{
UpdatePanel.ContentTemplateContainer.Controls.Add(((ControlCollection)HttpContext.Current.Session["owners"])
);
i--;
}
}
}
HttpContext.Current.Session.Remove("owners");
HttpContext.Current.Session.Add("owners",
UpdatePanel.ContentTemplateContainer.Controls);
}
}
In my UserControl in Page_Load method I have:
this.DelOwner.Click += new EventHandler(DelOwner_Click);
Could U help me what did I wrong?
BR,
Jack