Eventhandling from dynamically created controls

M

mrbentzen

I have some questions reguarding event handling of dynamically added
controls.

An example scenario (se code below):
I have one button declared in the aspx file. When the button is
clicked, the OnCommand event is fired.
The evanthandler catching this event, creates a new button dynamically
and ads it to a placeholders control collection.
This new button also has an event handler which ads another button etc.
But as this button does not exsist when the postback is processed, the
event from the second button cannot be caught.

Is there any way to be able to catch this event?

This is ofcouse a very simplified version of the real problem, the
solution consists of several, more complex databound control.
If it's possible, it is desired that the control is not databound more
than once, as thisfor som controls costs quite a bit of CPU time.

I know that you would normally just recreate the controls in Page.Load
o.a., but this is a quite complex solution with controls in controls
(etc.) and the page does therefore not know all the dynamically created
controls directly.
I have seen one solution where the controls are added to Session, but
this is an undesired solution in our case.

If, by any chance there are any Microsoft designers listening in, I
would like to ask what the official correct way to handle events from
dynamically created controls is. I have looked at the Paterns and
Practices pages, but not found any clear answer to the problem.

/Bentzen

*-----EventSample.aspx-----*
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="EventSample.aspx.cs" Inherits="test_EventSample" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="<%=Page.Request.Url %>">Reload</a><br /><br />
<asp:Button ID="Btn1" runat="server" Text="p1"
OnCommand="Ctrl_OnCommand"/>
<asp:placeHolder ID="PH1" runat="server"/>
</div>
</form>
</body>
</html>

*-----EventSample.aspx.cs-----*
public partial class test_EventSample : System.Web.UI.Page{
protected void Ctrl_OnCommand(object sender, CommandEventArgs e){
AddNewButton("New - " + ((Control)sender).ClientID, PH1);
}

private void AddNewButton(string name, PlaceHolder PH){
Button NewBtn = new Button();
NewBtn.Text = name;
NewBtn.Command += new CommandEventHandler(Ctrl_OnCommand);
PH.Controls.Add(NewBtn);
}
}
 
P

Phillip Williams

In your example below you could have preserved the event had you also
recreated the new button upon loading the page; which you could have done had
you saved any indicator in the Session object that identifies the added
control.

In this demo I create both server and HTML controls during handling of an
event but I recreated them upon Page_Load also:
http://www.societopia.net/Samples/DynamicallyCreatedControls.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top