PC Review


Reply
Thread Tools Rate Thread

Compisite Custom Controls - Event Handlers

 
 
Mr Newbie
Guest
Posts: n/a
 
      6th Nov 2005
Hi,

Im doing something really stupid here, But I cant see the wood for the
trees. This is some of the code for my custom composite control. It renders
fine and the buttons do cause a Post to occur. But the Click Handlers are
not are never receiving a click event. I note that the name of the control
is _ctl0 or 1 etc.

<span id="MyDamnButton1" style="width:192px;Z-INDEX: 101; LEFT: 16px;
POSITION: absolute; TOP: 24px"><input type="submit" name="_ctl0"
value="Submit Me" /><input type="submit" name="_ctl1" value="Submit Me 2"
/></span>

What could I have done wrong ?

------------------------

Protected btnSubmit As New System.Web.UI.WebControls.Button
Protected btnSubmit2 As New System.Web.UI.WebControls.Button

Protected Overrides Sub CreateChildControls()
btnSubmit.Text = "Submit Me"
btnSubmit2.Text = "Submit Me 2"
Controls.Add(btnSubmit)
Controls.Add(btnSubmit2)

AddHandler btnSubmit.Click, AddressOf Me.Click1Handler
AddHandler btnSubmit2.Click, AddressOf Me.Click1Handler2
End Sub


Private Sub Click1Handler(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim a As Int16 = 0

End Sub

Private Sub Click1Handler2(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim a As Int16 = 0
End Sub



--
Best Regards

The Inimitable Mr Newbie º¿º


 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      6th Nov 2005
Hi Mr. N,

Composite Controls can be tricky.

Let me refer you first to a page that I keep a printout on my wall of, for
ready reference, as it is often useful in developing ASP.Net applications:

http://msdn.microsoft.com/library/de...nLifecycle.asp

This article, on the "Control Execution Lifecycle," details the order in
which every System.Web.UI Control is processed. There is an interesting
omission in the chart, and that is the CreateChildControls() method.
However, at the very bottom of the page, there is a note about this
omission:

"The CreateChildControls method is not listed in the table because it is
called whenever the ASP.NET page framework needs to create the controls tree
and this method call is not limited to a specific phase in a control's
lifecycle. For example, CreateChildControls can be invoked when loading a
page, during data binding, or during rendering."

Fascinating, eh? The problem in your case is that the child controls that
you are wiring up event handlers for don't happen to exist when the event is
fired.

The solution is found in a method common to all System.Web.UI.Control
classes: EnsureChildControls(). This method checks to see whether child
controls have been created, and if not, calls the CreateChildControls()
method to create them.

There are numerous ways of making sure that the child controls are created
when the PostBack Event is raised. All of them require that the child
controls exist prior to the "Handle PostBack Events" phase occurs. In your
case, I would think that adding an override to the OnLoad method, and
calling EnsureChildControls() at that point would do the trick.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

"Mr Newbie" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
>
> Im doing something really stupid here, But I cant see the wood for the
> trees. This is some of the code for my custom composite control. It
> renders fine and the buttons do cause a Post to occur. But the Click
> Handlers are not are never receiving a click event. I note that the name
> of the control is _ctl0 or 1 etc.
>
> <span id="MyDamnButton1" style="width:192px;Z-INDEX: 101; LEFT: 16px;
> POSITION: absolute; TOP: 24px"><input type="submit" name="_ctl0"
> value="Submit Me" /><input type="submit" name="_ctl1" value="Submit Me 2"
> /></span>
>
> What could I have done wrong ?
>
> ------------------------
>
> Protected btnSubmit As New System.Web.UI.WebControls.Button
> Protected btnSubmit2 As New System.Web.UI.WebControls.Button
>
> Protected Overrides Sub CreateChildControls()
> btnSubmit.Text = "Submit Me"
> btnSubmit2.Text = "Submit Me 2"
> Controls.Add(btnSubmit)
> Controls.Add(btnSubmit2)
>
> AddHandler btnSubmit.Click, AddressOf Me.Click1Handler
> AddHandler btnSubmit2.Click, AddressOf Me.Click1Handler2
> End Sub
>
>
> Private Sub Click1Handler(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
> Dim a As Int16 = 0
>
> End Sub
>
> Private Sub Click1Handler2(ByVal sender As System.Object, ByVal e As
> System.EventArgs)
> Dim a As Int16 = 0
> End Sub
>
>
>
> --
> Best Regards
>
> The Inimitable Mr Newbie º¿º
>



 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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
Event handlers on nested custom controls in WPF sundarvenkata Microsoft Dot NET Framework 2 7th Jun 2009 07:47 PM
Event handlers on nested custom controls in WPF sundarvenkata Microsoft Dot NET 0 31st May 2009 12:52 PM
Event handlers on nested custom controls in WPF sundarvenkata Microsoft Dot NET 0 31st May 2009 12:52 PM
Event handlers and dynamically created controls =?Utf-8?B?TWlrZSBSYW5k?= Microsoft ASP .NET 2 26th Jan 2007 08:31 PM
How do you add event handlers for controls generated with xsl? MikeC Microsoft C# .NET 0 5th Jan 2004 09:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:03 PM.