tags:

views:

389

answers:

2

Does anyone know of a way to select all plain-html <p> elements in an ASP.net page, server-side? In my case, I'd like to apply a bit of text manipulation to all of them before they go to the browser.

I realize that I can add runat=server and then FindControl for each one. But that's a lot of code.

This would be the equivalent of javascript's getElementsByTagName, but server-side.

Thanks!

+1  A: 

Hi, you can use browser adapter for page: Just an idea: Derived class from System.Web.UI.Page -> MyPage

You should create BrowserAdapter and override render of this page, in output you will find generated HTML that will go to client browser. In this case you can make some XSLT mutations, or simple XML(XPATH) replacements and in a result you mission will be accomplished :) .

<browsers>
<browser refID="default">
 <controlAdapters>
  <adapter controlType="System.Web.UI.Page"
     adapterType="yournamespace.TestAdapter" />
 </controlAdapters>
</browser>

public class TestAdapter : PageAdapter
    {
     protected override void Render(HtmlTextWriter writer)
     {
      /* Get page output into string */
      var sb = new StringBuilder();
      TextWriter tw = new StringWriter(sb);
      var htw = new HtmlTextWriter(tw);

      // Render into my writer
      base.Render(htw);

      string page = sb.ToString();

                        // Here you can change output of render

      writer.Write(page);
     }
    }
omoto
might work, have you tested this? But XSLT/XPath is a bit far fetched, html is not xml by a long show, especially the output of server controls :(
Chad Grant
Sure it's working :) I'm not using XSLT usually for this actions. It's best way for pages localization.
omoto
A: 

Consider an Http Filter Module by filtering the output of your handlers.

There is no equivalent of document.getElementsByTagName on the server, unless every single p tag was runat=server.

http://msdn.microsoft.com/en-us/magazine/cc301704.aspx

Chad Grant