jsmith
jsmith

Reputation: 575

Dynamically deleting span tags while preserving inner contents

I'm creating a custom control to make label-input blocks for forms.

The following code works on its own:

<label id="custom_calendar_label" class="required" for="custom_calendar">Calendar thing: </label>
<input id="custom_calendar" type="date" required="required" aria-labelledby="custom_calendar_label">

but when I use a custom server control to build it, for whatever reason, span tags around the custom control get automatically generated (not from any code in my custom control),

so my code winds up looking like

<span id ="custom_calendar">

<label id="custom_calendar_label" class="required" for="custom_calendar">Calendar thing: </label>
<input id="custom_calendar" type="date" required="required" aria-labelledby="custom_calendar_label">

</span>

How do I dynamically delete the span tags, but preserve the label input contents inside the span tags?

Upvotes: 0

Views: 676

Answers (3)

Goran
Goran

Reputation: 393

You should try to use Literal control instead of the Label control.

Text from the Label control renders in HTML inside tags, but text from the Literal control renders as a plain text.

Upvotes: 0

Mantorok
Mantorok

Reputation: 5266

Have you tried overriding the TagKey?

   protected override HtmlTextWriterTag TagKey
   {
      get { return HtmlTextWriterTag.Unknown; }
   }

Upvotes: 0

Brian Mains
Brian Mains

Reputation: 50728

By default, it tries to render one parent control. So it's rendering a span as that parent tag. If your control inherits from web control, you can adjust the tagname to render a different control (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.tagname.aspx).

Or, since you don't want it, don't call base.Render() when you override the render method, and do:

protected virtual void OnRender(HtmlTextWriter writer)
{
    base.AddAttributesToRender(writer);

    //Render
    writer.RenderBeginTag("span");
    .
    .
}

I believe the wrapper element gets rendered with RenderBeginTag() and RenderEndTag(); you could try overriding those too and doing nothing.

What base class are you using too? Are you inheriting from Label?

Upvotes: 1

Related Questions