I’m trying to take an existing bunch of code that was previously on a full .aspx page, and do the same stuff in a .ashx handler.
The code created an HtmlTable object, added rows and cells to those rows, then added that html table the .aspx’s controls collection, then added it to a div that was already on the page.
I am trying to keep the code in tact but instead of putting the control into a div, actually generate the html and I’ll return that in a big chunk of text that can be called via AJAX client-side.
HtmlTable errors out when I try to use the InnerHtml property (says it isn’t supported), and when I try RenderControl, after making first a TextWriter and next an HtmlTextWriter object, I get the error that Page cannot be null.
Has anyone done this before? Any suggestions?
*Most recent is above.
OK, even after Matt’s update there is a workaround 😉
Firstly, we have to use a page with
forminside. Otherwise we won’t be able to add aScriptManagercontrol. One more thing: theScriptManagercontrol should be the first control in the form. Further is easier:This works. The output is quite large so I decided not to include it into my answer 🙂
Actually, there is a workaround. Yep, we may render a control in handler.
Firstly, we need a formless page. Because without it we get:
Secondly, nobody can prevent us from creating an instance of our
FormlessPagepage. And now let’s add a control there (I decided to add aButtoncontrol as an example, but you could use any).Thirdly, let’s capture the output. For this we use
HttpServerUtility.Executemethod:Here is the code:
The result will be:
<input type="submit" name="btnSumbit" value="TextButton" id="btnSumbit" />In addition I can recommend ScottGu’s article Tip/Trick: Cool UI Templating Technique to use with ASP.NET AJAX for non-UpdatePanel scenarios. Hope, you could find a lot of useful there.