EL323 EL323 - 1 year ago 66
HTML Question

How to navigate to a section on postback?

I have multiple sections on my aspx page.

<li class="tab-current"><asp:HyperLink ID="home" runat="server" Text="Home" NavigateUrl="#section-1"/></li>
<li><asp:HyperLink ID="events" runat="server" Text="Events" NavigateUrl="#section-2"/></li>
<li><asp:HyperLink ID="finance" runat="server" Text="Finance" NavigateUrl="#section-3"/></li>
<li><asp:HyperLink ID="merchandise" runat="server" Text="Merchandise" NavigateUrl="#section-4"/></li>
<li><asp:HyperLink ID="tasks" runat="server" Text="Tasks" NavigateUrl="#section-5"/></li>
<li><asp:HyperLink ID="profile" runat="server" Text="Profile" NavigateUrl="#section-6"/></li>

On one of the sections I have a button and a gridview control. On button click gridview gets loaded with some data, However button click causes postback and takes me Section#1. I want to stay on the that gridview after postback.


Answer Source

Add MaintainScrollPositionOnPostback="true" to the Page directive.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="myPage.aspx.cs" MaintainScrollPositionOnPostback="true" %>

Or if you want this for all your pages you could set in the Web.Config under the <system.web> node.

<pages validateRequest="true" maintainScrollPositionOnPostBack="true" enableViewStateMac="true" enableEventValidation="true" viewStateEncryptionMode="Always"/>


To make sure the correct tab is displayed after PostBack, you need to call a client side function.

First add something like this to the section onclick="setTabIndex(1)". This can also be onmouseover, a binding on class name etc.

<asp:HiddenField ID="HiddenField1" runat="server" />

<script type="text/javascript">
    function setTabIndex(tab) {
        //store the tab number in a hidden field so you can access it in code behind
        document.getElementById("<% = HiddenField1.ClientID %>").value = tab;

    function showTab(tab) {
        //open the correct tab

And in code behind call the client side function showTab:

        if (IsPostBack)
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "showTab", "showTab(" + HiddenField1.Value + ")", true);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download