Creating web server control and working with postbacks

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.
Who is Participating?
DropZoneConnect With a Mentor Commented:
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:

xersoftAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.