Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have an anchor tag inside listview that expands/hides a row inside the listview both are inside an updatePanel.
the anchor tage javascript code is the following:
<a onclick=" $(this).parent().parent().next().find('#notesTd').toggle();" href="#" id="notes_anch"><img src="../img/notes1.png" class="wiringImg" runat="server" id="notesImg" visible="false" width="40" /></a>

This is the row that the anchor tag show/hide:
<td colspan="6" style="display: none;" id="notesTd" class="showHideNotes">Notes
 <div id="divScroll">
                                                                            <asp:ListView runat="server" ID="notesView" DataSource="<%# Eval("NOTES", "{0}").Split('|') %>">
<LayoutTemplate>                                                                    
<ul>                                                                                   <li runat="server" id="itemPlaceholder" style="list-style: none;"></li>            
</ul>                                                                         </LayoutTemplate>
                                                                                <EmptyItemTemplate>no notes to display</EmptyItemTemplate>
                                                                                <ItemTemplate>                                                                            <li style="list-style: none; color: #000 !important;">                                 <%#string.Format("{0}",Container.DataItem) %>                                      
</li>                                                                              <asp:Label runat="server" ID="lblNotes" Visible="false" Text='<%#string.Format("{0}",Container.DataItem) %>'></asp:Label>                                      </ItemTemplate>                                                                            </asp:ListView>                                                                            </div>
</td>

It's working very well show/hides BUT the problem is I want the page to stay in the same position after anchor click but it keeps going to the top. I tried many solutions with no luck

any help will be very much appreciated
Thanks in advance

What I have tried:

1-design / C# solution
MaintainScrollPositionOnPostback="true" OR Page.MaintainScrollPositionOnPostBack = true; //on page load

2-JavaScript solution:
<script type="text/javascript">

        function SetScrollerPosition() {
            if (document.getElementById("XPos").value != "") {
                var x = document.getElementById("XPos").value;
                var y = document.getElementById("YPos").value;
                window.scrollTo(x, y);
            }

            document.getElementById("XPos").value = "0";
            document.getElementById("YPos").value = "0";
        }

        function GetScollerPosition() {
            var scrollX, scrollY;

            if (document.all) {
                if (!document.documentElement.scrollLeft)
                    scrollX = document.body.scrollLeft;
                else
                    scrollX = document.documentElement.scrollLeft;

                if (!document.documentElement.scrollTop)
                    scrollY = document.body.scrollTop;
                else
                    scrollY = document.documentElement.scrollTop;
            }
            else {
                scrollX = window.pageXOffset;
                scrollY = window.pageYOffset;
            }

            document.getElementById("XPos").value = scrollX;
            document.getElementById("YPos").value = scrollY;
        }

    </script>
<input type="hidden" id="XPos" runat="server" />
<input type="hidden" id="YPos" runat="server" />

3- another javascript solution (similar to #2)
<script type="text/javascript">
            var xPos, yPos;
            var prm = sys.WebForms.PageRequestManager.getInstance();

            function BeginRequestHandler(sender, args) {
                if ($get('<%=UpdatePanel1.ClientID%>') != null) {
                    xPos = $get('<%=UpdatePanel1.ClientID%>').scrollLeft;
                    yPos = $get('<%=UpdatePanel1.ClientID%>').scrollTop;
                }
            }

            function EndRequestHandler(sender, args) {
                if ($get('<%=UpdatePanel1.ClientID%>') != null) {
                    $get('<%=UpdatePanel1.ClientID%>').scrollLeft = xPos;
                    $get('<%=UpdatePanel1.ClientID%>').scrollTop = yPos;
                }
            }

            prm.add_beginRequest(BeginRequestHandler);
            prm.add_beginRequest(EndRequestHandler);
        </script>
Posted
Updated 28-Apr-17 3:47am
v2
Comments
Bryian Tan 17-Apr-17 22:36pm    
Please show relevant code. Where is the element with id notesTd? Where the anchor reside in?
Samira Radwan 18-Apr-17 9:38am    
both anchor and the (TD) inside a listview. notesTd is a table td please check the second block of code in my question. thank you!

managed to do it by adding Javascript:void
<a onclick=" $(this).parent().parent().next().find('#notesTd').toggle();" href="javascript:void(0);" id="notes_anch">


OR adding href="#!"
<a onclick=" $(this).parent().parent().next().find('#notesTd').toggle();" href="#!" id="notes_anch">


But i would suggest the first solution the reason why? check this link[^]
 
Share this answer
 
v2
 put the list view in a div with some id(divLstView1)
 
 function setScrollValue(divlistViewid) {
            var divObj=$("[id$=_" + divlistViewid + "]")[0];              
            var objX = $get('<%= hidScrollPosX.ClientID %>');
            var objY = $get('<%= hidScrollPosY.ClientID %>');

            if (objX && objY) {
                if (divObjX) {
                    objX.value = divObj.scrollLeft;
                    objY.value = divObj.scrollTop;
                }
            }
        }
		
		  
        function setScrollPageload(divlistViewid) {
            var divObj = $("[id$=_" + divlistViewid + "]")[0];           
            var objX = $get('<%= hidScrollPosX.ClientID %>');
            var objY = $get('<%= hidScrollPosY.ClientID %>');
            if (divObj) {
                if (objX && objY) {
                    divObj.scrollLeft = objX.value;
                    divObj.scrollTop = objY.value;
                }
            }
        }
		var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function (sender, e) {
		setScrollPageload('divLstView1');
	};
		function pageLoad()
		{
			setScrollPageload('divLstView1');
		}

//for the on click of anchor tag call the setScrollValue method
<a onclick="setScrollValue('divLstView1');$(this).parent().parent().next().find('#notesTd').toggle();" ....

		//put these hidden variables and javascript code in main page, not in the user control
		<input type="hidden" id="hidScrollPosX" runat="server" />
		<input type="hidden" id="hidScrollPosY" runat="server" />
 
Share this answer
 
v2
Comments
Samira Radwan 28-Apr-17 9:49am    
thanks a lot I managed to solve it. Please check my solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900