Creating web server control and working with postbacks

Posted on 2007-08-04
Last Modified: 2013-11-07
Id like to create a web user control that inherits from System.Web.UI.WebControls.WebControl.

I implement IPostbackDataHandler

In the RenderContents method, I do the following

writer.AddAttribute(HtmlTextWriterAttribute.Name, Me.UniqueID & "$test")
writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID & "_test")
writer.AddAttribute(HtmlTextWriterAttribute.Type, "textbox")

writer.AddAttribute(HtmlTextWriterAttribute.Name, Me.UniqueID & "$test2")
writer.AddAttribute(HtmlTextWriterAttribute.Id, Me.ClientID & "_test2")
writer.AddAttribute(HtmlTextWriterAttribute.Type, "textbox")

Notice the name of each textbox.

My implementation of the IPostbackDataHandler interface is as such:

    Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData
    End Function

    Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent
    End Sub

Very simple, just stopping the app when I click a button NOT contained in the user control.

The question is why does this not work? Why when I place this user control on a web form along with a button and run it do I not hit either of those methods when I click the button.

While looking at examples I noticed that the name of the child controls, the textboxes in this case, need to be named me.UniqueID. So I did this for one of the text boxes and reran the project. When I clicked the button NOT in the user control the LoadPostData method was run.

So I changed the second textboxs name to me.UniqueID and ran the example. It worked fine but the value found in the postCollection was a concatenated string containing both values found in the text boxes.

I dont understand how to create server controls that have multiple textboxes, which responds to post backs raised by unrelated submits.

The underlying question is how does one create server controls that rely only on the render method to output the underlying HTML but have the ability to respond to postbacks?

Thanks you for any help that can be offered.
Question by:xersoft
    LVL 18

    Accepted Solution

    First, I think you are confusing a few concepts here.

    1. The LoadPostData() event is only triggered when data has changed, and it is intended so that you have a chance to set the new data into the control's appropriate property.  For example, the TextBox checks if the new data that comes in is different, and if so, assigns it to the Text property.

    2. You should let the framework deal with IDs and ClientIDs as it knows how to guarantee they are unique.  You can assign IDs to your internal controls, but you should let the framework compose the final fully qualified strings.

    3. If you want your composite control to respond to events raised by its internal controls, then just use the OnBubbleEvent() event handler.  This method is called by all control events, and by default they "raise" the event (i.e. bubble up) to their parent, until one is found that stops the bubbling chain.

    Check out this site for more information on creating custom composite controls, and how to handle events within it:

    LVL 5

    Author Comment

    Yep your right, I was confused. I have read some more examples and also took your advice on letting the framework deal with Ids and client Ids. I use the load child controls overload now to the construction of my user control.

    Thanks for the help

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now