Mantener div mostrado en la devolución de datos

I have a web user control and a div inside it; some jQuery toggles div's visibility. There is also a asp:button that launches some server side code.

I need to restore div's visibility after postback.

I'm following the updated solution posted in the this post's solution. Here is my code:

At the top of the ascx, after the Registers:

<script>
function SetHiddenValue()
{        
   var campo = document.getElementById("<% =hidHiddenField.ClientID %>");
   if(campo.Value == "NO")
   {               
      document.getElementById("<% =hidHiddenField.ClientID %>").Value = "SI";  
   }
   else
   {
      document.getElementById("<% =hidHiddenField.ClientID %>").Value = "NO";
   }        
}
</script>

The div definition:

<div id="divContenidoMetricas" style='<%= DefinirVisibilidad() %>'>

In that same web user control:

<asp:ImageButton runat="server" ImageUrl="~/Themes/Images/buscar.PNG" OnClick="btnFiltrar_Click"
                    ID="btnFiltrar" OnClientClick="SetHiddenValue()" />

In the ascx.cs:

protected string DefinirVisibilidad()
{
   return this.hidHiddenField.Value == "SI" ? "display:block" : "display:none";
} 

It is just not working. hidHiddenField.Value arrives to the server code (DefinirVisibilidad()) with the same value all the times.

Thanks so much in advance for your help... my client side code knowledge is kind of damaged by ASP.NET so I'm stuck.

Resuelto

I replaced 'Value' with 'value' and that solved the problem. It's working!

preguntado el 08 de noviembre de 11 a las 17:11

If using ASP.NET Ajax you could use the AlwaysVisibleControl-Extender. -

Are you sure of your SetHiddenValue function? It just puts "SI" in the hidden field if its current value is "NO", and "NO" in all other cases... I don't get it. -

@Rodolphe, its like campo.Value = !campo.Value ... since hidden value is string instead of boolean, I did it that way. (SI = YES, NO = NO) -

@TimSchmelter, thanks for your suggestion. My problem is a server side button inside the hiding div. I need to mantain visibility status after postback, so using the AlwaysVisibleControl-Extender wont solve my problem. -

3 Respuestas

Prueba esto, por favor:

function SetHiddenValue()
{        
   var campo = document.getElementById("<% =hidHiddenField.ClientID %>");
   var display = document.getElementById("divContenidoMetricas").style.display;

   if(display == "block")
   {               
      campo.value = "SI";  
   }
   else
   {
      campo.value = "NO";
   }        
}

EDIT: Replaced "Value" with "value".

respondido 08 nov., 11:22

How about using jQuery to set it on document ready?

$(document).ready(function() {
  var showDiv = <%= DefinirVisibilidad() %>;
  if (showDiv != true) {
    $('#divContenidoMetricas').hide();
  }
});

Instead of setting the style directly using <% %> blocks.

respondido 08 nov., 11:21

From what I understood the problem is server-side. - Rodolphe

Value has to be lowercase, so it worked after replacing:

   if(campo.value == "NO")
   {               
      document.getElementById("<% =hidHiddenField.ClientID %>").value = "SI";  
   }
   else
   {
      document.getElementById("<% =hidHiddenField.ClientID %>").value = "NO";
   }   

respondido 15 nov., 11:20

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.