Tuesday, May 10, 2011

JavaScript – Persist state of page using Hidden field

function showHide(divId,ImgId)
{
divstyle = document.getElementById(divId).style.display;
Image_ID=document.getElementById(ImgId);
if(divstyle.toLowerCase()==”block” &&  document.getElementById(‘<%=hdnShowHide.ClientID %>’).value==”0″)
{
document.getElementById(‘<%=hdnShowHide.ClientID %>’).value=”1″;
Hide();
}
else
{
document.getElementById(divId).style.display=”block”;
Image_ID.src=”/Images/uparrows_white.gif”;
document.getElementById(‘<%=hdnShowHide.ClientID %>’).value=”0″;
}
}
function Hide()
{
Image_ID=document.getElementById(‘img2′);
if(document.getElementById(‘<%=hdnShowHide.ClientID %>’).value==”1″)
{
Image_ID.src=”/Images/downarrows_white.gif”;
document.getElementById(‘divSearchCriteria’).style.display=”none”;
}
}
<asp:HiddenField ID=”hdnShowHide” runat=”server” Value=”0″ />
For user control:
At end of control’s HTML:
<script type=”text/javascript”>
Hide();
</script>
For page using content of master:
Use below code in script after hide function:
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != ‘function’) {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(Hide);

No comments:

Post a Comment