S
studio60podcast
I have been fighting with this for almost two days and I can't figure
it out. I'm hoping someone can shed some light on my problem.
I have a web user control (NewAccountHolders) that contains a generic
list of another web user control (NewAccountHolder). I place
NewAccountHolders in a page and all renders properly, but the
_lnkRemove_Click never fires when I click "Remove". If I create
instances of NewAccountHolder outside of NewAccountHolders and add
them directly to the page, all works properly. But from within the
NewAccountHolders "container", they don't fire properly.
Can anyone see where I may have missed a step or wired something up
out of order?
test.aspx
<div>
<asp:TextBox ID="txtTest" runat="Server" />
<asplaceHolder ID="phTest" runat="server" />
</div>
test.aspx.cs
namespace MyTest
{
public delegate void AccountHolderRemoveEventHandler(int index);
public delegate void SomethingHappenedEventHandler(string value);
public partial class test2 : System.Web.UI.Page
{
protected NewAccountHolders _accountHolders;
protected void Page_Load(object sender, EventArgs e)
{
_accountHolders = new NewAccountHolders();
phTest.Controls.Add(_accountHolders);
_accountHolders.SomethingHappened += new
SomethingHappenedEventHandler(_accountHolders_SomethingHappened);
}
protected void _accountHolders_SomethingHappened(string value)
{
txtTest.Text = value;
}
}
public class NewAccountHolders : WebControl
{
public event SomethingHappenedEventHandler SomethingHappened;
public List<NewAccountHolder> AccountHolderList
{
get { return (HttpContext.Current.Session["account_holder_list"] ==
null) ? new List<NewAccountHolder>() :
(List<NewAccountHolder>)HttpContext.Current.Session["account_holder_list"]; }
set { HttpContext.Current.Session["account_holder_list"] = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
AddAccountHolder("User 1", "123-45-6579", "1/2/1903");
AddAccountHolder("User 2", "123-45-6579", "1/2/1903");
AddAccountHolder("User 3", "123-45-6579", "1/2/1903");
foreach (NewAccountHolder item in AccountHolderList)
{
item.AccountHolderRemoveClicked += new
AccountHolderRemoveEventHandler(NewRegistrationAccountHolders_AccountHolderRemoveClicked);
this.Controls.Add(item);
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
public void AddAccountHolder(string fullName, string id, string
dateOfBirth)
{
List<NewAccountHolder> list = AccountHolderList;
list.Add(new NewAccountHolder(fullName, id, dateOfBirth,
AccountHolderList.Count));
AccountHolderList = list;
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write("<table border=\"0\" cellpadding=\"0\" cellspacing=
\"0\" class=\"accountHolderContainer\">");
for (int i = 0; i < 3; i++)
{
AccountHolderList.RenderControl(output);
}
output.Write("</table>");
}
protected void
NewRegistrationAccountHolders_AccountHolderRemoveClicked(int index)
{
SomethingHappened(index.ToString());
}
}
public class NewAccountHolder : WebControl
{
private int _index;
private string _name, _id, _dob;
private LinkButton _lnkRemove;
public event AccountHolderRemoveEventHandler
AccountHolderRemoveClicked;
public NewAccountHolder(string name, string id, string dob, int
index)
{
_name = name;
_id = id;
_dob = dob;
_index = index;
_lnkRemove = new LinkButton();
_lnkRemove.Text = "Remove";
_lnkRemove.CssClass = "greysmall";
this.Controls.Add(_lnkRemove);
_lnkRemove.Click += new EventHandler(_lnkRemove_Click);
}
public int Index
{
get { return _index; }
set { _index = value; }
}
protected void _lnkRemove_Click(object sender, EventArgs e)
{
AccountHolderRemoveClicked(Index);
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write("<tr>");
output.Write("<td colspan=\"2\" class=\"accountHolderItem\">");
output.Write(_name + "<br>");
output.Write(_id + "<br>");
output.Write(_dob + "<br>");
output.Write("</td>");
output.Write("</tr>");
output.Write("<tr>");
output.Write("<td class=\"accountHolderRemove\">");
_lnkRemove.RenderControl(output);
output.Write("</td>");
output.Write("</tr>");
}
}
}
Thanks!
Jason
it out. I'm hoping someone can shed some light on my problem.
I have a web user control (NewAccountHolders) that contains a generic
list of another web user control (NewAccountHolder). I place
NewAccountHolders in a page and all renders properly, but the
_lnkRemove_Click never fires when I click "Remove". If I create
instances of NewAccountHolder outside of NewAccountHolders and add
them directly to the page, all works properly. But from within the
NewAccountHolders "container", they don't fire properly.
Can anyone see where I may have missed a step or wired something up
out of order?
test.aspx
<div>
<asp:TextBox ID="txtTest" runat="Server" />
<asplaceHolder ID="phTest" runat="server" />
</div>
test.aspx.cs
namespace MyTest
{
public delegate void AccountHolderRemoveEventHandler(int index);
public delegate void SomethingHappenedEventHandler(string value);
public partial class test2 : System.Web.UI.Page
{
protected NewAccountHolders _accountHolders;
protected void Page_Load(object sender, EventArgs e)
{
_accountHolders = new NewAccountHolders();
phTest.Controls.Add(_accountHolders);
_accountHolders.SomethingHappened += new
SomethingHappenedEventHandler(_accountHolders_SomethingHappened);
}
protected void _accountHolders_SomethingHappened(string value)
{
txtTest.Text = value;
}
}
public class NewAccountHolders : WebControl
{
public event SomethingHappenedEventHandler SomethingHappened;
public List<NewAccountHolder> AccountHolderList
{
get { return (HttpContext.Current.Session["account_holder_list"] ==
null) ? new List<NewAccountHolder>() :
(List<NewAccountHolder>)HttpContext.Current.Session["account_holder_list"]; }
set { HttpContext.Current.Session["account_holder_list"] = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
AddAccountHolder("User 1", "123-45-6579", "1/2/1903");
AddAccountHolder("User 2", "123-45-6579", "1/2/1903");
AddAccountHolder("User 3", "123-45-6579", "1/2/1903");
foreach (NewAccountHolder item in AccountHolderList)
{
item.AccountHolderRemoveClicked += new
AccountHolderRemoveEventHandler(NewRegistrationAccountHolders_AccountHolderRemoveClicked);
this.Controls.Add(item);
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
public void AddAccountHolder(string fullName, string id, string
dateOfBirth)
{
List<NewAccountHolder> list = AccountHolderList;
list.Add(new NewAccountHolder(fullName, id, dateOfBirth,
AccountHolderList.Count));
AccountHolderList = list;
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write("<table border=\"0\" cellpadding=\"0\" cellspacing=
\"0\" class=\"accountHolderContainer\">");
for (int i = 0; i < 3; i++)
{
AccountHolderList.RenderControl(output);
}
output.Write("</table>");
}
protected void
NewRegistrationAccountHolders_AccountHolderRemoveClicked(int index)
{
SomethingHappened(index.ToString());
}
}
public class NewAccountHolder : WebControl
{
private int _index;
private string _name, _id, _dob;
private LinkButton _lnkRemove;
public event AccountHolderRemoveEventHandler
AccountHolderRemoveClicked;
public NewAccountHolder(string name, string id, string dob, int
index)
{
_name = name;
_id = id;
_dob = dob;
_index = index;
_lnkRemove = new LinkButton();
_lnkRemove.Text = "Remove";
_lnkRemove.CssClass = "greysmall";
this.Controls.Add(_lnkRemove);
_lnkRemove.Click += new EventHandler(_lnkRemove_Click);
}
public int Index
{
get { return _index; }
set { _index = value; }
}
protected void _lnkRemove_Click(object sender, EventArgs e)
{
AccountHolderRemoveClicked(Index);
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write("<tr>");
output.Write("<td colspan=\"2\" class=\"accountHolderItem\">");
output.Write(_name + "<br>");
output.Write(_id + "<br>");
output.Write(_dob + "<br>");
output.Write("</td>");
output.Write("</tr>");
output.Write("<tr>");
output.Write("<td class=\"accountHolderRemove\">");
_lnkRemove.RenderControl(output);
output.Write("</td>");
output.Write("</tr>");
}
}
}
Thanks!
Jason