Solved

Remember which row was made visible with JavaScript

Posted on 2011-02-15
4
558 Views
Last Modified: 2012-06-27
Hi,

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 asp.net "remember" the currently visible div tag.  

Thanks

0
Comment
Question by:yechan
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:P1ST0LPETE
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.

$('#hfSelectedRow').val(rowNumber);

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.

$(document).ready(function()
{
       if ($('#hfSelectedRow').val() > -1)
       {
               $('#NestedDiv' + $('#hfSelectedRow').val()).attr('display', inline);
       }
});

You get the idea.
0
 
LVL 1

Author Comment

by:yechan
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);

thanks
0
 
LVL 10

Accepted Solution

by:
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'
0
 
LVL 1

Author Comment

by:yechan
ID: 34908818
This is beautiful.

thank you.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now