当其他字段变得可见时,HTML跳转到页面顶部

时间:2016-12-28 13:32:43

标签: asp.net

我的页面中有一些cs,当选择子弹时,会显示一个文本框,提供有关选择原因的信息。我遇到的问题是,当选择子弹并出现文本框时,页面总是跳到顶部,用户必须向下滚动。

这也会导致我的文件上载字段在文本开始上传时消失,然后在页面的其余部分更改项目符号。

有人知道如何阻止它跳转并保护文件上传字段不会重置数据吗?

<p>
<asp:Label ID="lblstoretooling" runat="server" Text="If no, why?"></asp:Label>
<asp:TextBox ID="txtstoretooling" runat="server" height="50px" TextMode="MultiLine" Width="600px"></asp:TextBox>
</p>
<br>
<p>Did you delete program(s) from the "To Machine" folder?
<asp:RadioButtonList ID="isdeletedprogram" runat="server" AutoPostBack="True" OnSelectedIndexChanged="isdeletedprogram_SelectedIndexChanged" RepeatDirection="Horizontal" style="position: relative;">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
</p>

CS

if (isdeletedprogram.SelectedValue == "No")
    {
        txtdeletedprogram.Visible = true;
        lbldeletedprogram.Visible = true;
    }

1 个答案:

答案 0 :(得分:0)

这很可能是由自动回发引起的:

<asp:RadioButtonList ID="isdeletedprogram" runat="server" 
                     AutoPostBack="True" OnSelectedIndexChanged="isdeletedprogram_SelectedIndexChanged"
                     RepeatDirection="Horizontal" style="position: relative;">

单击单选按钮时,页面会发生完整的PostBack。换句话说:您的网页会重新加载,您的所有进度都会丢失。您必须手动保存要保留的对象的当前状态。

所以你需要做的是创建一个方法,在Session,ViewState或Cache中保存所有元素状态,并在页面加载时从中加载所有元素。

//call this one in your isdeletedprogram_SelectedIndexChanged 
protected void SavePageState()
{
    Session["IsDeletedState"] = isdeletedprogram.SelectedIndex;
    /*...*/
}

//call this on in the else clause - when a postback occurs
protected void LoadPageState()
{
    isdeletedprogram_SelectedIndexChanged = Session["IsDeletedState"];
    /*add any server side controls state you want to save here*/
}

你的Page.Load会看起来像这样

protected void Page_Load()
{
    if(!Page.IsPostback)
    {
        //do anything you would normally do
    }
    else
    {
        //this will get executed on a Postback. Load data here
        LoadPageState()
    }
}

由您决定在此选择哪种持久性内存。 Session,Cache,ViewState ......