简体   繁体   English

ASP.NET:使用AutoPostBack时如何阻止页面跳转?

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

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. 我正在使用ASP.NET,在向导控件中,我具有单选按钮,如果选择“是”,则显示一个面板,但是如果选择“否”,则该面板是隐藏的。 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. 我将MaintenanceScrollPositionOnPostBack设置为True,尽管ActiveStepChanged事件将其更改为False,以便单击“下一步”以查看向导的下一个步骤时,它将在页面顶部开始。 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? 如何阻止它第一次跳?

Set Page.MaintainScrollPositionOnPostBack = true to maintain the current screen position on Postback. 设置Page.MaintainScrollPositionOnPostBack = true可以保持当前屏幕在回发时的位置。

This is easier than trying to do this yourself through JavaScript or whatever means. 这比尝试通过JavaScript或其他方式自己完成操作要容易得多。

通过将控件放在UpdatePanel中,可以阻止页面进行完全刷新

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. 我知道已经解决了这个问题,但是我无法在我正在处理的项目中使用ajax,但遇到了类似的问题,并且找到了可接受的解决方法。 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). 主页上的主要内容div使其可滚动(因为它太大)。 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. 我发现的解决方案是将页面焦点设置为控制由c#代码背后的回发触发的事件。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM