即使使用更新面板,页面也会跳转到页面顶部

时间:2014-02-13 09:28:07

标签: c# asp.net ajax updatepanel

我遇到了一些问题。我在更新面板中包含了一些asp.net控件,但是当我单击提交按钮时,它会跳转到页面顶部。我在这里阅读了很多帖子,他们要么使用一些javascript,要么在页面指令中将MaintainPagePostion设置为“true”。我尝试将其设置为true,但这不起作用。我真的不想使用javascript脚本来实现这一点。我的印象是这是使用更新面板的好处之一。然而,我发现最令人困惑的部分是,它曾经不习惯这样做。我不记得在网站上更改会导致此问题的任何内容。任何有关此问题的帮助表示赞赏。感谢。

这是我正在使用的代码。

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
    <asp:Panel ID="pnlEmailStuff" runat="server">
        Name: <asp:TextBox ID="txtName" runat="server" Width="202px"></asp:TextBox><br />
        Email: <asp:TextBox ID="txtEmail" runat="server" Width="203px"></asp:TextBox><br />
                <span style="font-size:12px; font-weight:normal; margin-left:55px;">**Please double check email**</span><br />
        Message:<br />
        <asp:TextBox ID="txtMessage" runat="server" Width="370px" TextMode="MultiLine" Font-Names="Tahoma" Font-Size="Small" Height="75px"></asp:TextBox><br />
        <asp:Label ID="lblEmailError" runat="server" Text="" Font-Size="Small" ForeColor="Red"></asp:Label> 
        <asp:ImageButton Height="25px" Width="60px" CssClass="EmailSubmit" ImageUrl="Images/MailingListBtnSubmit2.png" ID="btnSubmit" runat="server" onclick="btnSubmit_Click"/>
    </asp:Panel>
    <asp:Panel ID="pnlThankYou" runat="server" Visible="false">
        <p style="text-align:center; font-size:30px;">Thank you!<br /><span style="font-size:20px;">Your Email has been sucessfully submitted.</span></p>
    </asp:Panel>
</ContentTemplate>

6 个答案:

答案 0 :(得分:4)

你可以用4种方式做到:

  1. 来自代码隐藏 - Page.MaintainScrollPositionOnPostBack = true;

  2. 来自Page Directive - MaintainScrollPositionOnPostback="true"

  3. 来自Web.config - <pages maintainScrollPositionOnPostBack="true" />

  4. 使用Javascript。您可以使用以下链接中的代码。它对我有用 -

  5. http://weblogs.asp.net/andrewfrederick/archive/2008/03/04/maintain-scroll-position-after-asynchronous-postback.aspx

答案 1 :(得分:1)

我认为,它并没有跳到页面顶部。它刷新了页面。你的更新面板的UpdateMode是什么?有条件的吗?如果是有条件的,请检查触发器。 ControlID应为按钮ID,EventName ='Click'。然后检查更新面板的区域。

示例代码:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
    <asp:Panel ID="pnlEmailStuff" runat="server">
        Name: <asp:TextBox ID="txtName" runat="server" Width="202px"></asp:TextBox><br />
        Email: <asp:TextBox ID="txtEmail" runat="server" Width="203px"></asp:TextBox><br />
                <span style="font-size:12px; font-weight:normal; margin-left:55px;">**Please double check email**</span><br />
        Message:<br />
        <asp:TextBox ID="txtMessage" runat="server" Width="370px" TextMode="MultiLine" Font-Names="Tahoma" Font-Size="Small" Height="75px"></asp:TextBox><br />
        <asp:Label ID="lblEmailError" runat="server" Text="" Font-Size="Small" ForeColor="Red"></asp:Label> 
        <asp:ImageButton Height="25px" Width="60px" CssClass="EmailSubmit" ImageUrl="Images/MailingListBtnSubmit2.png" ID="btnSubmit" runat="server" onclick="btnSubmit_Click"/>
    </asp:Panel>
    <asp:Panel ID="pnlThankYou" runat="server" Visible="false">
        <p style="text-align:center; font-size:30px;">Thank you!<br /><span style="font-size:20px;">Your Email has been sucessfully submitted.</span></p>
    </asp:Panel>
</ContentTemplate>
<Triggers>
             <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>

答案 2 :(得分:0)

您是否在更新面板中指定了触发器?如果您在触发器部分中指定触发器,则更新面板将更新而不会跳到顶部。此外,您需要提供 updatemode =“conditional”。可以这样做:

<asp:UpdatePanel ID="ex" runat="server" UpdateMode="Conditional">
<ContentTemplate>
//your controls here
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="yourbuttonid" />
</Triggers>
</asp:UpdatePanel>

答案 3 :(得分:0)

非常感谢给我建议的每个人。事实证明,Page Routing是导致此问题的原因。所以我要做的就是在RegisterRoutes代码块中添加并忽略该页面的行:

    void RegisterRoutes(RouteCollection routes)
    {
        routes.Ignore("Mobile");
        routes.Ignore("Booking.aspx*");//<---- This Fixed it.
        routes.MapPageRoute("Gallery", "Gallery/{Name}", "~/Gallery.aspx");
        routes.Ignore("*");//<---- This is better for me. It acts as a catch all.
    }

这有助于我解决问题:http://forums.asp.net/t/1743640.aspx

修改

我添加了&#34; routes.Ignore(&#34; &#34;);&#34;代码,它作为一个捕获所有,所以我真的不需要忽略&#34; Booking.aspx&#34;特别。请记住,虽然订单在这里很重要。忽略(&#34; &#34;)需要是最后一个,否则其他路由都不起作用。

再次感谢大家。

答案 4 :(得分:0)

请尝试使用PageRequestManager处理程序

<script>
              Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);



        function EndRequestHandler(sender, args) {
            try {

                sender._controlIDToFocus = null;


            }
            catch (e) {
            }
        }

</script>

答案 5 :(得分:0)

我因类似问题被困了几个小时。我确信问题出在 UpdatePanel ,但最终是表单标签中的 defaultfocus defaultbutton 属性,导致页面跳转最多将第一个文本框聚焦在页面顶部。

<form id="form1" runat="server" defaultbutton="buttonId" defaultfocus="textboxId">

面对此类问题时,这是另一件事。

相关问题