0

In ASP.Net (4.5.2) I have nested <span> elements where the parent is a set as a server side control...

<p>
  This is the start of the text
  <span runat="server" visible="<%#someCode%>">
    This is some more Text
    <span class="myClass">with some other text inside</span>
    And a bit more after
  </span>
</p>

(Note, this is contained within a <asp:Repeater>)

It appears that ASP.Net doesn't cope with this, and appears to assume the inner </span> is the closure for the outer element. Meaning that when the visible="false" it will render like this...

<p>
  This is the start of the text
    And a bit more after
  </span>
</p>

I cannot convert either <span> into a <div> or <section> as it has to live within a <p> element (meaning the child elements cannot be blocks).

Is there any way to get around this issue?

freefaller
  • 19,368
  • 7
  • 57
  • 87

2 Answers2

1

OK then (too many comments).

Alternatively use a Label:

<p>
    This is the start of the text
    <asp:Label ID="Label1" runat="server" Visible="false" Text="This is some more Text">
        <span class="myClass">with some other text inside</span>
        And a bit more after
    </asp:Label>                    
</p>

or even:

<p>
    This is the start of the text
    <asp:Label ID="Label1" runat="server" Visible="true" Text="This is some more Text<span class='myClass'>with some other text inside</span>And a bit more after">                
    </asp:Label>                    
</p>
dpant
  • 1,622
  • 19
  • 30
  • 1
    I was concerned that the tag within the `Text` attribute would result in a `badly formed tag` error, but it appears to also work correctly – freefaller Jun 06 '19 at 13:38
0

Turns out the answer is quite simple... make the inner <span> a server-side control as well...

<p>
  This is the start of the text
  <span runat="server" visible="<%#someCode%>">
    This is some more Text
    <span runat="server" class="myClass">with some other text inside</span>
    And a bit more after
  </span>
</p>

Which results in ...

<p>
  This is the start of the text
</p>
freefaller
  • 19,368
  • 7
  • 57
  • 87
  • Sounds like a bad idea to me, now you have the overhead of another server-side control. – DavidG Jun 06 '19 at 12:41
  • Yes @DavidG, I thought the same thing... but I can't figure out any other way of doing it – freefaller Jun 06 '19 at 12:42
  • Does it need to be a `span`? Use a `div` or `section` or whatever. – DavidG Jun 06 '19 at 12:42
  • @DavidG, as I said in the question, I can't use a `
    ` because it needs to be contained within a `

    ` element... I'll investigate `

    `

    – freefaller Jun 06 '19 at 12:44
  • @DavidG - nope, `
    ` acts as a block element, taking it outside of the `

    ` in the same way `

    ` does.
    – freefaller Jun 06 '19 at 12:46
  • Then don't use a `p` tag? – DavidG Jun 06 '19 at 12:50
  • @DavidG - unfortunately not possible in this current situation – freefaller Jun 06 '19 at 12:51
  • So you can change span to div but not p to div? Well OK then! – DavidG Jun 06 '19 at 13:01
  • @DavidG - your profile says you've been in the industry for a while (as have I) and therefore I assume you'll be aware there are times when things could be done correctly if you had the time/energy/money/enthusiasm... and there are times you just can't – freefaller Jun 06 '19 at 13:07
  • Haha sure, but why is changing a single tag deemed such a time consuming or expensive operation? I suppose it doesn't matter, it's your code. :) – DavidG Jun 06 '19 at 13:09
  • @DavidG - I'd tell you, but it's `TL;DR` ;-) Thanks for your input though, it's honestly appreciated – freefaller Jun 06 '19 at 13:10
  • @freefaller Interestingly enough, I am trying to reproduce but your original sample seems to work OK. (I've placed the spans inside `ItemTemplate`). So you may want to check your other markup for orphans. – dpant Jun 06 '19 at 13:16
  • Thanks @dpant but it's definitely in the block as I've described it. Not sure I can justify much more time on this, as I do have a solution, even if it's not the best one – freefaller Jun 06 '19 at 13:19