Modifying content of RenderControl

Discussion in 'ASP .Net Building Controls' started by Samuel Hon, Apr 29, 2004.

  1. Samuel Hon

    Samuel Hon Guest

    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
    Samuel Hon, Apr 29, 2004
    #1
    1. Advertising

  2. Samuel Hon

    ccallen Guest

    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

    "Samuel Hon" <> wrote in message
    news:...
    > 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
    ccallen, Apr 29, 2004
    #2
    1. Advertising

  3. Samuel Hon

    Samuel Hon Guest

    Hi there

    Thanks for the reply.

    "ccallen" <> wrote in message news:<#>...
    > 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?
    Samuel Hon, Apr 29, 2004
    #3
  4. 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" <> wrote in message
    news:...
    > Hi there
    >
    > Thanks for the reply.
    >
    > "ccallen" <> wrote in message

    news:<#>...
    > > 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?
    Fred Hirschfeld, Apr 30, 2004
    #4
  5. Samuel Hon

    Samuel Hon Guest

    Thanks Fred

    Thats just the ticket

    "Fred Hirschfeld" <> wrote in message news:<>...
    > 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" <> wrote in message
    > news:...
    > > Hi there
    > >
    > > Thanks for the reply.
    > >
    > > "ccallen" <> wrote in message

    > news:<#>...
    > > > 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?
    Samuel Hon, Apr 30, 2004
    #5
  6. Samuel Hon

    Samuel Hon Guest

    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
    Samuel Hon, May 4, 2004
    #6
  7. 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

    "Samuel Hon" <> wrote in message
    news:...
    > 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
    Fred Hirschfeld, May 6, 2004
    #7
  8. Samuel Hon

    Samuel Hon Guest

    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

    "Fred Hirschfeld" <> wrote in message news:<>...
    > 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
    >
    Samuel Hon, May 6, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George Ter-Saakov

    Render vs RenderControl

    George Ter-Saakov, Nov 10, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    19,976
    sobin
    Oct 25, 2006
  2. Kathy Burke
    Replies:
    2
    Views:
    3,704
    Kathy Burke
    Nov 11, 2003
  3. Steve Chatham

    Datagrid to Excel - error - rendercontrol

    Steve Chatham, Feb 23, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    454
    Steve Chatham
    Feb 23, 2004
  4. Ken Boone via .NET 247

    form RenderControl doesn't work in win app

    Ken Boone via .NET 247, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    779
    Norman Yuan
    Sep 22, 2004
  5. hazz
    Replies:
    6
    Views:
    49,416
    SkyUCHC
    Jun 9, 2010
Loading...

Share This Page