Compisite Custom Controls - Event Handlers

  • Thread starter Thread starter Mr Newbie
  • Start date Start date
M

Mr Newbie

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
 
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/d...guide/html/cpconControlExecutionLifecycle.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.
 
Back
Top