473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Delegates, Events and the Page Lifecycle

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" />
<asp:PlaceHolder 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_Some thingHappened);
}

protected void _accountHolders_SomethingHappened(string value)
{
txtTest.Text = value;
}
}

public class NewAccountHolders : WebControl
{
public event SomethingHappenedEventHandler SomethingHappened;

public List<NewAccountHolderAccountHolderList
{
get { return (HttpContext.Current.Session["account_holder_list"] ==
null) ? new List<NewAccountHolder>() :
(List<NewAccountHolder>)HttpContext.Current.Sessio n["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(NewRegistrationAcc ountHolders_AccountHolderRemoveClicked);
this.Controls.Add(item);
}
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}

public void AddAccountHolder(string fullName, string id, string
dateOfBirth)
{
List<NewAccountHolderlist = 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[i].RenderControl(output);
}
output.Write("</table>");
}

protected void
NewRegistrationAccountHolders_AccountHolderRemoveC licked(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

Apr 13 '07 #1
5 1582
Try changing the declaration of the LinkButton to protected:

protected LinkButton _lnkRemove;

"st*************@gmail.com" wrote:
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" />
<asp:PlaceHolder 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_Some thingHappened);
}

protected void _accountHolders_SomethingHappened(string value)
{
txtTest.Text = value;
}
}

public class NewAccountHolders : WebControl
{
public event SomethingHappenedEventHandler SomethingHappened;

public List<NewAccountHolderAccountHolderList
{
get { return (HttpContext.Current.Session["account_holder_list"] ==
null) ? new List<NewAccountHolder>() :
(List<NewAccountHolder>)HttpContext.Current.Sessio n["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(NewRegistrationAcc ountHolders_AccountHolderRemoveClicked);
this.Controls.Add(item);
}
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}

public void AddAccountHolder(string fullName, string id, string
dateOfBirth)
{
List<NewAccountHolderlist = 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[i].RenderControl(output);
}
output.Write("</table>");
}

protected void
NewRegistrationAccountHolders_AccountHolderRemoveC licked(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

Apr 13 '07 #2
Nope, didn't help.

Apr 13 '07 #3
I apologize for any incorrect guesses - I'm trying to guess at some of what
your code is doing since I program in VB.

Anyway, the next thing I'd try is to narrow it down. Try creating a single
class-level object (instead of a list), change your various NewAccountHolders
methods to use the single instance, and then see if you're able to get the
event to fire - hopefully that will help you narrow down the possibilities.
Apr 13 '07 #4
Hi,

controls containing child controls need to implement INamingContainer
interface (or derive from CompositeControl).

And it's not really smart to keep control instances on Session as you have
the list of AccountHolders. Having the list itself is OK, but you shouldn't
put control instances there directly but items like ListItems are (e.g just
data containers), However, I removed it entiorely since the list wasn't used
for anything else (and as child controls were instantiated directly in code,
no need to store them into session etc)

Here's the modified code:

public class NewAccountHolders : WebControl,INamingContainer
{

public event SomethingHappenedEventHandler SomethingHappened;
protected override void CreateChildControls()
{
NewAccountHolder h = new NewAccountHolder("User 1",
"123-45-6579", "1/2/1903", 0);
h.AccountHolderRemoveClicked += new
AccountHolderRemoveEventHandler(NewRegistrationAcc ountHolders_AccountHolderRemoveClicked);
Controls.Add(h);

h = new NewAccountHolder("User 2", "123-45-6579", "1/2/1903",
1);
h.AccountHolderRemoveClicked += new
AccountHolderRemoveEventHandler(NewRegistrationAcc ountHolders_AccountHolderRemoveClicked);
Controls.Add(h);

h = new NewAccountHolder("User 3", "123-45-6579", "1/2/1903",
2);
h.AccountHolderRemoveClicked += new
AccountHolderRemoveEventHandler(NewRegistrationAcc ountHolders_AccountHolderRemoveClicked);
Controls.Add(h);

}

protected override void RenderChildren(HtmlTextWriter output)
{
output.Write("<table border=\"0\" cellpadding=\"0\"
cellspacing=\"0\" class=\"accountHolderContainer\">");
for (int i = 0; i < Controls.Count; i++)
{
Controls[i].RenderControl(output);
}
output.Write("</table>");

}
protected void
NewRegistrationAccountHolders_AccountHolderRemoveC licked(int index)
{
SomethingHappened(index.ToString());
}
}

public class NewAccountHolder : WebControl,INamingContainer
{
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;
}

protected override void CreateChildControls()
{
_lnkRemove = new LinkButton();
_lnkRemove.ID = "remove";
_lnkRemove.Text = "Remove";
_lnkRemove.CssClass = "greysmall";
_lnkRemove.Click += new EventHandler(_lnkRemove_Click);
this.Controls.Add(_lnkRemove);

}
public int Index
{
get { return _index; }
set { _index = value; }
}

protected void _lnkRemove_Click(object sender, EventArgs e)
{
if (AccountHolderRemoveClicked != null)
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>");
}
}
--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net

<st*************@gmail.comwrote in message
news:11*********************@o5g2000hsb.googlegrou ps.com...
>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" />
<asp:PlaceHolder 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_Some thingHappened);
}

protected void _accountHolders_SomethingHappened(string value)
{
txtTest.Text = value;
}
}

public class NewAccountHolders : WebControl
{
public event SomethingHappenedEventHandler SomethingHappened;

public List<NewAccountHolderAccountHolderList
{
get { return (HttpContext.Current.Session["account_holder_list"] ==
null) ? new List<NewAccountHolder>() :
(List<NewAccountHolder>)HttpContext.Current.Sessio n["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(NewRegistrationAcc ountHolders_AccountHolderRemoveClicked);
this.Controls.Add(item);
}
}

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}

public void AddAccountHolder(string fullName, string id, string
dateOfBirth)
{
List<NewAccountHolderlist = 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[i].RenderControl(output);
}
output.Write("</table>");
}

protected void
NewRegistrationAccountHolders_AccountHolderRemoveC licked(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
Apr 15 '07 #5
Teemu,

Thanks for the reply! Your suggestions fixed my problem. When
attempting to store the NewAccountHolder in ViewState, I hadn't
thought of it as storing the actualy webcontrol, but rather just the
data. So, I created a separate class for the data, store that in
ViewState, then use it to bind to new instances of NewAccountHolder.

I also had never implemented INamingContainer either, so this was a
beneficial problem all around!

Thanks again!
Jason

Apr 19 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: V | last post by:
I have found that when I have a composite control that uses the CreateChildControls method, on a regular page load, Page_Load executes before CreateChildControls, but on a postback it is the...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
2
by: AC | last post by:
I have a page where I'm displaying articles based on the current year selected. My page dynamically builds a few links at the top to select a different year than the current one selected (the...
2
by: RAJ | last post by:
In our multi-tier application, we have several ASP.NET user controls which will update the same data source provided by middle tier logic. In this particular scenario we have one user control...
7
by: Dominique Vandensteen | last post by:
I have a Table to which I add a LinkButton in the PreRender event The LinkButton is visible on the webpage but when I click it, the LinkButton_Click method is not called and the page just "reloads"...
1
by: Petr SIMUNEK | last post by:
I have 3 buttons on the page. (Created dynamicaly inside For- next loop and hooked up to click event.) When different button is clicked i would like to save a different value to viewstate. In the...
6
by: Steve Hershoff | last post by:
Hi everyone, I've got a strange one here. There are two datagrids on my page, one nested within the other. I'll refer to them as the topmost and secondary datagrids. In the topmost...
2
by: Gary W. Smith | last post by:
I have a page that inherits from a base page that is currently overriding all of the On* events. For the most part I'm accomplishing everything I set out to do with the inheritance, but I wanted...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.