Change presence of ASP.NET tag with JavaScript

I have a ASP.NET web page with an asp: switch that is not noticeable. I can not transform it noticeable with JavaScript due to the fact that it is not provided to the web page.

What can I do to settle this?

2019-05-18 21:31:32
Source Share
Answers: 5

If you wait till the web page is filled, and afterwards set the switch is display screen to none, that need to function. After that you can make it noticeable at a later factor.

2019-05-21 04:37:07

If you require to adjust it on the customer side, you can not make use of the Visible building on the server side. Rather, set its CSS display design to "none". As an example:

<asp:Label runat="server" id="Label1" style="display: none;" />

Then, you can make it noticeable on the customer side with:

document.getElementById('Label1').style.display = 'inherit';

You can make it concealed once more with:

document.getElementById('Label1').style.display = 'none';

Keep in mind that there might be concerns with the ClientID being extra intricate than "Label1" in technique. You'll require to make use of the ClientID with getElementById, not the server side ID, if they vary.

2019-05-21 04:33:21

Make sure the Visible building is readied to real or the control will not provide to the web page. After that you can make use of manuscript to adjust it.

2019-05-21 04:30:49

Continuing with what Dave Ward claimed:

  • You can not set the Visible building to incorrect due to the fact that the control will certainly not be provided.
  • You need to make use of the Style building to set it is display screen to none .

Page/Control layout

<asp:Label runat="server" ID="Label1" Style="display: none;" />

<asp:Button runat="server" ID="Button1" />

Code behind

Somewhere in the load area:

Label label1 = (Label)FindControl("Label1");
((Label)FindControl("Button1")).OnClientClick = "ToggleVisibility('" + label1.ClientID + "')";

Javascript documents

function ToggleVisibility(elementID)
    var element = document.getElementByID(elementID);

    if ( = 'none')
    { = 'inherit';
    { = 'none';

Of training course, if you do not intend to toggle yet simply to show the button/label after that readjust the javascript method as necessary.

The vital factor below is that you require to send the details concerning the ClientID of the control that you intend to adjust on the customer side to the javascript documents either setup international variables or via a function parameter as in my instance.

2019-05-19 23:33:46

Try this.

<asp:Button id="myButton" runat="server" style="display:none" Text="Click Me" />

<script type="text/javascript">
    function ShowButton() {
        var buttonID = '<%= myButton.ClientID %>';
        var button = document.getElementById(buttonID);
        if(button) { = 'inherit'; }

Don't make use of web server - side code to do this since that would certainly call for a postback. As opposed to making use of Visibility = "incorrect", you can simply set a CSS building that conceals the switch. After that, in javascript, switch over that building back whenever you intend to show the switch once more.

The ClientID is made use of due to the fact that it can be various from the web server ID if the switch is inside a Naming Container control. These include Panels of numerous types.

2019-05-19 22:51:31