0

I have a some ASP.NET table rows that are initially invisible. On a click I am running a Javascript function that is changing the style of the table rows showing it or hiding it.

To show it :

document.getElementById(id).style.display = ""; 

To hide it :

document.getElementById(id).style.display = "none";

and this works.

The problem is that I need to make some filtering with the contents of the row if and only if the row is visible.

In code behind the style keeps state as was designed and does not takes the current state.

What will be the best approach to get the information if the element is visible or not?

Nope
  • 22,147
  • 7
  • 47
  • 72
Patrik
  • 1,286
  • 1
  • 31
  • 64
  • If you are refering to rows as in `tr` in a `table`. Can you please post the relevant HTML markup. What DOM element does `document.getElementById(id)` refer to ? – Nope Mar 25 '13 at 14:31
  • Can you show us the relevant surrounding code where you need to check the style? Knowing what you've currently got usually helps. – Anthony Grist Mar 25 '13 at 14:36
  • Could this question be a duplicate of: [**how-to-check-if-an-element-is-really-visible-with-javascript**](http://stackoverflow.com/questions/704758/how-to-check-if-an-element-is-really-visible-with-javascript) ? Not sure if this article helps but it certainly is interesting and seems to address this topic: [**javascript-tool-detect-if-a-dom-element-is-truly-visible**](http://useallfive.com/javascript-tool-detect-if-a-dom-element-is-truly-visible/) – Nope Mar 25 '13 at 15:16
  • 1
    @FrançoisWahl Based on the last two sentences ("*In code behind the style keeps state as was designed and does not takes the current state. What will be the best approach to get the information if the element is visible or not?*"), I think the OP is trying to check (on the server side) if the visibility was changed on the client side. Although I definitely see how you could interpret the question the way you did. It's a little unclear. – Josh Darnell Mar 25 '13 at 15:23
  • 1
    @jadarnel27: That's why I didn't hit the `close` button as duplicate but only asked in comments :) – Nope Mar 25 '13 at 15:25

2 Answers2

0

When you run that JavaScript, you need to change the value of something in a server control (that will postback to the server with the new value). If you don't already have something in place, you could use something as simple as a HiddenField control.

So you'd add this to your markup:

<asp:HiddenField ID="isRowShowing" Value="True" runat="server" />

And then do this in your JavaScript functions:

//To show
document.getElementById(id).style.display = "";
document.getElementById('<%= isRowShowing.ClientID %>').value = "True";

//To Hide
document.getElementById(id).style.display = "none";
document.getElementById('<%= isRowShowing.ClientID %>').value = "False";

Then you can check it in your code behind:

if(isRowShowing.Value == "True")
{
    // Do stuff
}
else
{
    // Do other stuff.  Or don't do stuff.  Whatevs.
}
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
0

To show it:

document.getElementById(id).style.display="inline";

To hide it:

document.getElementById(id).style.display="none";

To check it:

if(document.getElementById(id).style.display=="inline") {
    //Do your stuff
    }

Or am I missing something?

ElPedro
  • 576
  • 10
  • 17
  • 1
    Based on the last two sentences ("*In code behind the style keeps state as was designed and does not takes the current state. What will be the best approach to get the information if the element is visible or not?*"), I think the OP is trying to check (on the server side) if the visibility was changed on the client side. – Josh Darnell Mar 25 '13 at 15:22
  • Ah, perhaps I was missing something then :-\ – ElPedro Mar 25 '13 at 15:36