Reputation: 3902
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.
SOLVED
I replaced 'Value' with 'value' and that solved the problem. It's working!
Upvotes: 1
Views: 5155
Reputation: 3902
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";
}
Upvotes: 0
Reputation: 1731
Try this, please:
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".
Upvotes: 1
Reputation: 906
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.
Upvotes: 0