Modifying content of RenderControl

S

Samuel Hon

Hi

I'm currently using

Public Class Template
Inherits Control

Protected Overrides Sub Render(Writer As HtmlTextWriter)
Writer.WriteLine ("...")
Controls(1).RenderControl(Writer)
Writer.WriteLine ("...")
End Sub

End Class

in one of my controls.

I would like to modify the content in the control using regular
expressions. Does any one have any suggestions on how to do this?
Thanks

Sam
 
C

ccallen

Rather then writing each chunk to Writer.WriteLine(), build the string up in
a string object (or stringbuilder). Once the string has been constructed
(including regular expression operations), pass it to Writer.Writeline().

ccallen
 
S

Samuel Hon

Hi there

Thanks for the reply.

ccallen said:
Rather then writing each chunk to Writer.WriteLine(), build the string up in
a string object (or stringbuilder). Once the string has been constructed
(including regular expression operations), pass it to Writer.Writeline().

I thought about this, but the text i need to perform regex operations
on, is in the control and this only accepts HTMLTextWriters. I havent
yet discovered if I can manipulate the HTML in the text writer. Does
any one have any examples or ideas?
 
F

Fred Hirschfeld

I have not done this but you should be able to create a StringWriter object
and use that to create the HtmlTextWriter for memory writing. Then you can
use the text is has to do your regular expression stuff...

Note... this is C#...

StringBuilder sb = new StringBuilder()
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);

Controls[1].RenderControl(htw);

htw.Close();
sw.Close();

// use your Regular Expression code here...

Fred
 
S

Samuel Hon

Thanks Fred

Thats just the ticket

Fred Hirschfeld said:
I have not done this but you should be able to create a StringWriter object
and use that to create the HtmlTextWriter for memory writing. Then you can
use the text is has to do your regular expression stuff...

Note... this is C#...

StringBuilder sb = new StringBuilder()
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);

Controls[1].RenderControl(htw);

htw.Close();
sw.Close();

// use your Regular Expression code here...

Fred



Samuel Hon said:
Hi there

Thanks for the reply.

"ccallen" <[email protected]> wrote in message Writer.Writeline().

I thought about this, but the text i need to perform regex operations
on, is in the control and this only accepts HTMLTextWriters. I havent
yet discovered if I can manipulate the HTML in the text writer. Does
any one have any examples or ideas?
 
S

Samuel Hon

OK, I'm experiencing something strange now. The aspx rendering is
working in reverse order

In my aspx page, i'm using:

<SH:Template runat="server">
<SH:Container id="SH2" runat="server">
<B>Render something here.</B>
<% Response.Write("Something going on here.") %>
</SH:Container>
</SH:Template>

In my control, I'm using:

Namespace SH
Public Class Template
Inherits Control

Protected Overrides Sub Render(Writer As HtmlTextWriter)
Writer.WriteLine ("...")
Writer.WriteLine(ReplaceText(Controls(1),strText))
Writer.WriteLine ("...")
End Sub

Public Function ReplaceText(objControl As Control, _
strText As String) As String

Dim objStringWriter As New System.IO.StringWriter()
Dim objHTMLWriter As New HtmlTextWriter(objStringWriter)

objControl.RenderControl(objHTMLWriter)

Dim str As String = objStringWriter.ToString()
Return str.ToString()
End Function

End Class
End Namespace

What I'm finding is that my HTML page is coming up as

Something going on here. Render something here.

instead of

Render something here. Something going on here.

Any ideas? Cheers
 
F

Fred Hirschfeld

I have not done something like this but I am assuming that you cannot use
the inline code to do what you are expecting as I think the inline will get
executed and sent to the stream before your processing as appears to happen.

Then your control evaluates and renders its content. I would expect that you
might want to have a custom tag that can contain the information you would
like to write or add it as an attribute to the Container tag.

Also, do you need the runat=server for the sub-tag SH:Container?

Fred
 
S

Samuel Hon

Hi Fred

I'm afriad the third party assemblies I'm using dont allow me to place
my content in the attributes. To get around this, I've placed all my
content in the script <% %>. Not the most efficient for static
content, but its a workaround until I find a nice solution

Thanks for the help

Sam

PS You're right about the second runat=server, its not necessary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top