I have an ASP.Net
application that has a UserControl
that needs to be rendered on an ASPX
page (as normal), but also via Ajax
using a Handler.
One solution to this is to get the UserControl
to simply build up a HTML
string
, rather than rendering controls as it would normally.
That way, the ASPX
page can simply populate a Literal
control with the output, while the Handler can write the output to the Response object for Ajax
to pick up.
Is this the ideal solution? are there any drawbacks of this approach?
If you do that, you have to recreate the user control every time from the client-side when a postback to the server happens. However, performance wise, that may be better. However, I don't think the UC can then participate with viewstate and the ASP.NET object model as a typical user control would. You would have to account that all actions within the UC are handled appropriately, to react to the web.
If you have a large amount of content to render, the UC approach is just fine. We're talking rendering markup and injecting it into the UI, and so that does pan out and can make it easier. If it's not that much markup, any alternative would do.
If all you need to do is push data to the client, an alternative could be that you push JSON to the client through a web service, and build the UI on the client. That's efficient too.