I dynamically add a number of user controls to a page when the user presses a button on the main page. Each control has a remove button on it which should remove that specific user control. However since the page_load on the
host page fires first - before the remove button's code, the removal doesn't take effect until an additonal post back occurs.
Could someone please let me know how to get around this? Can I force the host page to perform a post back from within the user control - i.e perform teh removal code and then programmatically cause the host to post back.
I have seperated the HTML from the C# into two files and made a couple of changes because I'm still using 1.1.
If I add a single LinkButton, I can remove it by clicking on it. If however I add two it raises an exception when I click on either of the link buttons.
I get the following error (I have given the controls that are added a random ID):
Multiple controls with the same ID '1937201507' were found. FindControl requires that controls have unique IDs.
JG2
Member
60 Points
12 Posts
Removing user control
Dec 07, 2005 02:08 PM|LINK
Hi,
I dynamically add a number of user controls to a page when the user presses a button on the main page. Each control has a remove button on it which should remove that specific user control. However since the page_load on the host page fires first - before the remove button's code, the removal doesn't take effect until an additonal post back occurs.
Could someone please let me know how to get around this? Can I force the host page to perform a post back from within the user control - i.e perform teh removal code and then programmatically cause the host to post back.
Any help would be appreciated.
Cheers
sbyard
Contributor
5891 Points
1196 Posts
Re: Removing user control
Dec 07, 2005 06:03 PM|LINK
Try this
<%@ Page Language="C#" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><
script runat="server">System.Collections.Generic.List<Panel> list = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
list = new System.Collections.Generic.List<Panel>();
else
list = (System.Collections.Generic.List<Panel>)Session["TheList"];
foreach (Panel p in list)
{
pnlControls.Controls.Add(p);
foreach (Control c in p.Controls)
if (c is LinkButton)
{
((LinkButton)c).Click += new EventHandler(b_Click);
break;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Panel p = new Panel();
pnlControls.Controls.Add(p);
TextBox t = new TextBox();
p.Controls.Add(t);
LinkButton b = new LinkButton();
b.Text = "Remove";
b.Click += new EventHandler(b_Click);
p.Controls.Add(b);
list.Add(p);
}
void b_Click(object sender, EventArgs e)
{
LinkButton b = (LinkButton)sender;
b.Parent.Parent.Controls.Remove(b.Parent);
list.Remove((Panel)b.Parent);
}
protected void Page_PreRender(object sender, EventArgs e)
{
Session["TheList"] = list;
}
</
script><
html xmlns="http://www.w3.org/1999/xhtml"><
head runat="server"><title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button2" runat="server" Text="Test Postback" />
<div>
<asp:Button ID="Button1" runat="server" Text="Add Control" OnClick="Button1_Click" />
<asp:Panel ID="pnlControls" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>
JG2
Member
60 Points
12 Posts
Re: Removing user control
Dec 08, 2005 09:29 AM|LINK
Thanks for that, but I cannot get it to work. Is this is a ASP.NET 2.0 solution, or should it work with 1.1 also?
Basically it complains about the line: System.Collections.Generic.List<Panel> list = null;
Cheers,
Jon
sbyard
Contributor
5891 Points
1196 Posts
Re: Removing user control
Dec 08, 2005 11:36 AM|LINK
JG2
Member
60 Points
12 Posts
Re: Removing user control
Dec 08, 2005 03:50 PM|LINK
Thanks for that. It kinda works.
I have seperated the HTML from the C# into two files and made a couple of changes because I'm still using 1.1.
If I add a single LinkButton, I can remove it by clicking on it. If however I add two it raises an exception when I click on either of the link buttons.
I get the following error (I have given the controls that are added a random ID):
Multiple controls with the same ID '1937201507' were found. FindControl requires that controls have unique IDs.
Any ideas what's going on?
Cheers!
sbyard
Contributor
5891 Points
1196 Posts
Re: Removing user control
Dec 08, 2005 05:58 PM|LINK
Check your id assigment algorithm (and also look in the browser source at the id's generted there).
This works OK in VS2005 for unique ID's
b.ID = Guid.NewGuid().ToString("D");
JG2
Member
60 Points
12 Posts
Re: Removing user control
Dec 12, 2005 02:04 PM|LINK