Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Responding to events in dynamically created web controls

Reply
Thread Tools

Responding to events in dynamically created web controls

 
 
JezB
Guest
Posts: n/a
 
      06-18-2004
I'm adding WebControl objects to a Page dynamically on Page_Load, but I'm
having trouble attaching events to these. For example, adding an image
button :-

ImageButton nb = new ImageButton();
nb.ImageUrl = "text.gif";
nb.ToolTip = "Edit Text";
nb.Click += new ImageClickEventHandler(b1_Click);
myPlaceholder.Controls.Add(nb);

(myPlaceholder is a Placeholder object added to the Page at design time)

private void b1_Click(object sender, ImageClickEventArgs e)
{
// etc
}

Problem is, on clicking the button the event is not fired at all - it just
forces a postback - what am I doing wrong ?



 
Reply With Quote
 
 
 
 
Joe.Dattilo
Guest
Posts: n/a
 
      06-18-2004
The Server Side Event Handling happens before the page_load event and after the On_init ... add your controls and event handler hookups in the On_Init Event and you should be able to get the events to fire.

Joe

"JezB" wrote:

> I'm adding WebControl objects to a Page dynamically on Page_Load, but I'm
> having trouble attaching events to these. For example, adding an image
> button :-
>
> ImageButton nb = new ImageButton();
> nb.ImageUrl = "text.gif";
> nb.ToolTip = "Edit Text";
> nb.Click += new ImageClickEventHandler(b1_Click);
> myPlaceholder.Controls.Add(nb);
>
> (myPlaceholder is a Placeholder object added to the Page at design time)
>
> private void b1_Click(object sender, ImageClickEventArgs e)
> {
> // etc
> }
>
> Problem is, on clicking the button the event is not fired at all - it just
> forces a postback - what am I doing wrong ?
>
>
>
>

 
Reply With Quote
 
 
 
 
Robert Koritnik
Guest
Posts: n/a
 
      06-21-2004
This is OF COURSE NOT true. All events happen AFTER Page_Load event. The
thing is, that you developers sometimes don't know how the process happens
in the first place.
All dynamic controls should be restored the same way they were created the
first time. And you have to recreate them BEFORE the events get processed.
So the latest possible time to recreate those is Page_Load.
What JoeDattilo suggested is the way to process ViewState, not events.
Because viewstate gets processed after OnInit and BEFORE PageLoad. If you
don't need that, then forget about it...

--
RobertK
{ Clever? No just smart. }

"Joe.Dattilo" <> wrote in message
news:2066D479-C30C-4C3D-9D18-...
> The Server Side Event Handling happens before the page_load event and

after the On_init ... add your controls and event handler hookups in the
On_Init Event and you should be able to get the events to fire.
>
> Joe
>
> "JezB" wrote:
>
> > I'm adding WebControl objects to a Page dynamically on Page_Load, but

I'm
> > having trouble attaching events to these. For example, adding an image
> > button :-
> >
> > ImageButton nb = new ImageButton();
> > nb.ImageUrl = "text.gif";
> > nb.ToolTip = "Edit Text";
> > nb.Click += new ImageClickEventHandler(b1_Click);
> > myPlaceholder.Controls.Add(nb);
> >
> > (myPlaceholder is a Placeholder object added to the Page at design time)
> >
> > private void b1_Click(object sender, ImageClickEventArgs e)
> > {
> > // etc
> > }
> >
> > Problem is, on clicking the button the event is not fired at all - it

just
> > forces a postback - what am I doing wrong ?
> >
> >
> >
> >



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
Handling Events for Dynamically Created Controls Nathan Sokalski ASP .Net 2 05-23-2005 12:03 PM
Events for Dynamically created controls =?Utf-8?B?Qm9i?= ASP .Net 1 09-03-2004 03:35 AM
Adding events to dynamically created controls JezB ASP .Net 2 06-18-2004 02:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57