I have a custom control which is rendered as a hyperlink:
Public Class TestControl
Inherits System.Web.UI.WebControls.WebControl
Implements IPostBackEventHandler
Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
Trace.WriteLine("Hyperlink 1 clicked")
End Sub
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
writer.WriteLine("<a href=""{0}"" id=""{1}"">Hyperlink 1</a>", _
Page.ClientScript.GetPostBackClientHyperlink(Me, "Hyperlink 1"), _
Me.ClientID)
End Sub
End Class
This works fine. It also works nicely when put inside an UpdatePanel: Only the UpdatePanel is refreshed, no full postback is performed.
Now I would like to output a second hyperlink in the Render method. If I use the same id (Me.ClientID), everything works nicely, but this obviously results in broken HTML (no two controls are allows to have the same id attribute). If I use different client IDs (like Me.ClientID & "_1" and Me.ClientID & "_2"), a full postback is performed when the hyperlinks are clicked.
Is there some way to tell ASP.NET AJAX: “All postbacks of the following client IDs should be done asynchronously: …“?
I use a slightly different approach, which has been
UpdatePanel-friendly in my experience, which you may want to consider.Instead of generating HTML content directly in the
Render()method, I inherit my control fromCompositeControland fill it with child controls in theCreateChildControls()method. With the controls collection populated, the render method sort of “takes care of itself.”To the Update Panel, this composite control is just a bucket of otherwise-ordinary server controls, so the built-in behavior “just works.”
If you haven’t worked much with composite controls, here’s a resource to get started. I tend to build the whole control tree rather than override the
RenderControl()method, but it’s a reasonable resource anyway.