Wednesday, April 04, 2007

WebControl, post backs and INamingContainer

Ok, here is the first proof that since I got an MVP I didn't get any smarter...
I spent the last hour trying to figure out why the button in my control (inheriting from System.Web.UI.WebControls.WebControl) is not triggering the click event.

If you remember, a few days ago I posted a long post about how to properly handle server controls inside a web part and I lectured on the use of CreateChildControls and when to create the control. But in my WebControl it just didn't work!
Changing the inheritance to WebPart solved the issue, but I wanted a web control!

Finally a co-worker (Thanks Adam) reminded me that I had to implement the INamingContainer interface for the WebControl to support child controls properly. So I just added "WebCustomControl1 : WebControl, INamingContainer" to the class definition, and that's it!
I can't believe I forgot that, since I once found it the hard way, and I am almost sure I posted a tip about this before.

2 comments:

  1. Anonymous2:58 AM

    This works great, I just came across the same issue and would never have figured this out as the solution! Thanks alot.

    ReplyDelete
  2. Anonymous8:24 AM

    Yes, I've been having trouble with this all day, wasn't sure what was up with a fairly simple control.

    thanks for the help!

    ReplyDelete

Do not use comments to ask me questions. Want to ask me something, use the contact form