Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access span id in code behind

I have an asp website with the following control:

    <span id="expTrainingShow" class="clsLink" style="margin-left: 20px;" onclick="GridChanger();">
        + Show Expired Continuing Education</span>

I want to hide this based on a condition set in the code behind. Can I access a span id like that? (the website is built using visual basic)

like image 330
NealR Avatar asked Sep 05 '12 21:09

NealR


People also ask

Can you put ID in a span HTML?

Just as it is possible to style content by wrapping a span tag around it, you can also manipulate your content by wrapping it in a span tag. You give it an id attribute and then select it by its id with JavaScript so you can manipulate it.

How do I show span in HTML?

The <span> tag is easily styled by CSS or manipulated with JavaScript using the class or id attribute. The <span> tag is much like the <div> element, but <div> is a block-level element and <span> is an inline element.

How do you find the text of a span element?

Use the textContent property to get the text of a span element, e.g. const text = span. textContent . The textContent property will return the text content of the span and its descendants. If the element is empty, an empty string is returned.


1 Answers

You can use a Label instead of a html-span (which is also rendered as span) or you could add runat="server". Setting runat="server" allows you to access the HTML element in the code behind just as any other server control, via its ID.

<span id="expTrainingShow" runat="server" class="clsLink" style="margin-left: 20px;" onclick="GridChanger();" ></span>

somewhere in codebehind(the span is a HtmlGenericControl on serverside):

expTrainingShow.InnerHtml = yourText ' set the text '

or

expTrainingShow.Visible = False ' hide it '

Note that Visible=False on serverside means that the control is not rendered at all on clientside, hence it does not exist in the html and can be accessed only on serverside.

If you just want to hide it but render it anyway, you should use CSS or expTrainingShow.Style.Add("display","none").

like image 94
Tim Schmelter Avatar answered Sep 21 '22 01:09

Tim Schmelter