Remember which row was made visible with JavaScript

Posted on 2011-02-15
Last Modified: 2012-06-27

not sure whether to post this in the JavaScript section or here.  Anyways, here it goes.

I have a gridView nested inside a GridView. The child gridView has been embedded inside a div tag.  I have written a JavaScript that toggles the visiblity of the div tag on or off.  That works fine.  However, when the page is posting back, the currently visible tag become invisible again.  How can I make "remember" the currently visible div tag.  


Question by:yechan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 10

Expert Comment

ID: 34902793
Place a hidden field somewhere on your page, probably at the top of the gridview.

<asp:HiddenField ID="hfSelectedRow" runat="server" value="-1" />

In your whenever you use javascript to make a div visable, update this hiddenfield with the row number.


Then on postback have javascript that runs to check if the hidden field holds a value greater than -1, and if it does then make that div visible.

       if ($('#hfSelectedRow').val() > -1)
               $('#NestedDiv' + $('#hfSelectedRow').val()).attr('display', inline);

You get the idea.

Author Comment

ID: 34906838
Good morning,

thank you very much for your response.  If I am not mistaken that's jQuery syntax that you have.  I don't think the marines will allow me to install jQuery library will let me install the the library or make a reference to it.  I have much of it converted into "regular" javascript.  Need additional assistance on how to convert the following into non-jQuery syntax

$('#NestedDiv' + $('#hfSelectedRow').val()).attr('display', inline);

LVL 10

Accepted Solution

P1ST0LPETE earned 500 total points
ID: 34908408
Sorry, I was hesitant when I wrote the script to not use jQuery, but it's used so often these days I went ahead and did it anyway.  Anyway, below is the same script using standard JavaScript.

Note: In both the jQuery above and the script below, I am assuming that the ID's on your <div> tags that you are trying to hide/show are named "NestedDiv1" "NestedDiv2" "NestedDiv3", etc. with the number at the end of the ID corresponding with the grid row that it is in.

Here is the specific line you wanted:

document.getElementById('NestedDiv' + selectedRow).style.display = 'inline';

And here is the standard JavaScript version of the jQuery posted in my original post:

window.onload = function ()
    var selectedRow = document.getElementById('hfSelectedRow').value;

    if (selectedRow > -1)
        document.getElementById('NestedDiv' + selectedRow).style.display = 'inline';

To hide the div you can use:    style.display = 'none'

Author Comment

ID: 34908818
This is beautiful.

thank you.

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question