How can I generate a System.Web.UI.HtmlControls.HtmlTextArea with whatever attributes I want?

561 Views Asked by At

I have a very unique situation where I need to generate a <textarea> on the page, from code behind:

Public Class Textarea
  Inherits System.Web.UI.HtmlControls.HtmlTextArea
End Class

And the control:

<me:Textarea ... />

The problem is, my unique case requires me to use strange attributes. I basically need it to output something like this:

<textarea :class="{}" :id="something">

So I want to be able to do:

<me:Textarea :class="{}" etc. />

Is there an uncomplicated way to allow the rendering of exactly as I type? Or should I use another control? Build it from scratch in the render phase? I get errors about the tag not being well formed, so ultimately I just want it to spit out the <textarea> tag with the guts of it exactly as typed. Including C# in tags because doesn't matter. Will take any example.

2

There are 2 best solutions below

0
On BEST ANSWER

All of these answers are excellent, but I wanted to share with everyone what I figured out how to do to make it as simple as possible.

I just created a control:

Public Class Wrapper
  Inherits Control
End Class

Then:

<me:Wrapper>
  <textarea anything i want here... ></textarea>
</me:Wrapper>

Which just spits it out exactly as I typed. For some who question why even bother, I'm within another parser that uses a control builder and <textarea> is one of the child controls of it, so needed a workaround to just use regular HTML.

0
On

Try the following. It's not too bad.

For your server side control, create a Property that you can fill with whatever text you want. I called it "ExtraStuff". Support it with a Private variable. Then override the Render to write out your extra stuff as part of the tag:

    Public Class Textarea
    Inherits System.Web.UI.HtmlControls.HtmlTextArea

    Private m_sExtraStuff As String = ""

    Public Property ExtraStuff As String
        Get
            Return m_sExtraStuff
        End Get
        Set(value As String)
            m_sExtraStuff = value
        End Set
    End Property

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        writer.Write("<textarea ")
        writer.Write(m_sExtraStuff)
        writer.Write("></textarea>")
    End Sub

End Class

Then, in your markup you can add the extra stuff that you want to write out by adding it to the property you created. You'll have to make sure it's encoded properly though:

<me:Textarea runat="server" ExtraStuff=":class=&quot;{}&quot; :id=&quot;something&quot;"></me:Textarea>

When this renders, I think it will look like you want it:

<textarea :class="{}" :id="something"></textarea>

Or:

Another approach that might be more robust is to explicitly declare these special attributes and then write them out in your format. The server side control:

    Public Class Textarea
    Inherits System.Web.UI.HtmlControls.HtmlTextArea

    Private m_sSpecialID As String = ""
    Private m_sSpecialClass As String = ""

    Public Property SpecialID As String
        Get
            Return m_sSpecialID
        End Get
        Set(value As String)
            m_sSpecialID = value
        End Set
    End Property

    Public Property SpecialClass As String
        Get
            Return m_sSpecialClass
        End Get
        Set(value As String)
            m_sSpecialClass = value
        End Set
    End Property

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        writer.Write("<textarea :class=""")
        writer.Write(m_sSpecialClass)
        writer.Write(""" :id=""")
        writer.Write(m_sSpecialID)
        writer.Write("""></textarea>")
    End Sub

End Class

The markup:

<me:Textarea runat="server" SpecialClass="{}" SpecialID="something"></me:Textarea>

The output is the same.