ASP.NET: how to stop page from jumping when using AutoPostBack?

9.9k Views Asked by At

I'm using ASP.NET, and in a Wizard control I have radio buttons where if "Yes" is selected, a panel is shown, but if "No" is selected, the panel is hidden. I have MaintainScrollPositionOnPostBack set to True, though the ActiveStepChanged event changes it to False so that when you click Next to see the next Wizard step, it will start at the top of the page. The problem is, after clicking Next, the first time you click on a radio button it jumps to the top of the page (the page retains its position any time you click a radio button after the first time). How do I stop it from jumping the first time?

3

There are 3 best solutions below

2
On BEST ANSWER

You can stop the page from doing a full refresh by putting your control in an UpdatePanel

0
On

I know this has been answered, but I am unable to use ajax for the project I was working on I was having a similar issue and I found an acceptable work-around. I have a user control within a page that also has a master page. In the user control there is an input form that has a few controls near the bottom that trigger postback. The main content div on the master page makes this for scrollable (because it is too large). The solution that I found to it was setting the page focus to a control on the events triggered by postback in the c# code-behind. For example:

protected void cbShip_CheckedChanged(object sender, EventArgs e)    
{
    if (cbShip.Checked)
    {
        pnlShip.Visible = true;
        Page.SetFocus(ddlShipCountry);
    }
    else
    {
        pnlShip.Visible = false;
    }
    return;
}
1
On

Set Page.MaintainScrollPositionOnPostBack = true to maintain the current screen position on Postback.

This is easier than trying to do this yourself through JavaScript or whatever means.