简体   繁体   English

单击asp.net链接按钮将焦点设置在页面顶部

[英]Setting focus on top of the page on click of asp.net link button

I've a asp.net datagrid which shows customer order details. 我有一个显示客户订单详细信息的asp.net数据网格。 Pagination at the bottom of the grid is done using datalist and asp.net Linkbutton controls. 使用数据列表和asp.net Linkbutton控件可以完成网格底部的分页。 Here is the code: 这是代码:

<asp:DataList ID="DataList2" runat="server" CellPadding="1" CellSpacing="1" 
        OnItemCommand="DataList2_ItemCommand" 
        OnItemDataBound="DataList2_ItemDataBound" RepeatDirection="Horizontal">
    <ItemTemplate>
        <asp:LinkButton ID="lnkbtnPaging" runat="server" 
             CommandArgument='<%# Eval("PageIndex") %>'
             CommandName="lnkbtnPaging" 
             Text='<%# Eval("PageText") %>' />
        <asp:Label runat="server" ID="lblPageSeparator" Text=" | " name=></asp:Label>
    </ItemTemplate>
</asp:DataList>

When the user clicks on any page number(ie.Link button), I need to set focus on top of the page. 当用户单击任何页码(即“链接”按钮)时,我需要将焦点放在页面顶部。

How do i do this? 我该怎么做呢?

Thanks! 谢谢!

I think the default behaviour would be for the page scroll position to be set back to the top of the page. 我认为默认行为是将页面滚动位置设置回页面顶部。 Is there anything else in your page that might be overriding this behaviour? 您的页面中还有其他东西可能会覆盖此行为吗?

For example: 例如:

  1. Is your DataList inside an UpdatePanel? 您的DataList是否在UpdatePanel中? In that case the current scroll position will be maintained over a (partial) post-back. 在这种情况下,当前滚动位置将在(部分)回退后保持不变。 You would therefore need to reset the scroll position to the top of the page yourself. 因此,您需要自己将滚动位置重置到页面顶部。 One way to do this would be to implement a handler for the PageRequestManager's EndRequest client-side event which would set the scroll position - this thread explains how 一种实现方法是为PageRequestManager的EndRequest客户端事件实现一个处理程序,该处理程序将设置滚动位置-该线程说明
  2. Is the Page MaintainScrollPositionOnPostBack property set to true? 页面MaintenanceScrollPositionOnPostBack属性是否设置为true?

You could try setting a named anchor at the top of the page. 您可以尝试在页面顶部设置命名锚。 Here is an article that explains it http://www.w3schools.com/HTML/html_links.asp 这是一篇说明它的文章http://www.w3schools.com/HTML/html_links.asp

After an AJAX partial postback you may need to return to the top of your ASPX page to display an error message, etc. Here is one way that I have done it. 在进行AJAX部分回发之后,您可能需要返回到ASPX页面的顶部以显示错误消息等。这是我完成此操作的一种方法。 You can add the JavaScript function below to your ASPX page and then call the method when needed in your code-behind by using the ScriptManager.RegisterClientScriptBlock method. 您可以将下面的JavaScript函数添加到ASPX页面,然后在需要的时候通过使用ScriptManager.RegisterClientScriptBlock方法在代码隐藏中调用该方法。 ASP.NET C# Code-behind: ASP.NET C#代码背后:

ScriptManager.RegisterClientScriptBlock(this, Page.GetType(), 
 "ToTheTop", "ToTopOfPage();", true);

JavaScript: JavaScript的:

<script type="text/javascript">
function ToTopOfPage(sender, args) {
    setTimeout("window.scrollTo(0, 0)", 0);
}

You can also just JavaScript to scroll to the top of the page by using the OnClientClick property of your button. 您还可以使用按钮的OnClientClick属性将JavaScript滚动到页面顶部。 But this will cause this behavior to occur every time the button is clicked and not just when you want it to happen. 但是,这将导致这种现象在每次单击按钮时发生,而不仅仅是在您希望它发生时发生。 For example: <asp:Button id="bntTest" runat="server" Text="Test" OnClick="btn_Test" OnClientClick="javascript:window.scrollTo(0,0);" /> 例如: <asp:Button id="bntTest" runat="server" Text="Test" OnClick="btn_Test" OnClientClick="javascript:window.scrollTo(0,0);" /> <asp:Button id="bntTest" runat="server" Text="Test" OnClick="btn_Test" OnClientClick="javascript:window.scrollTo(0,0);" />

<asp:LinkButton ID="lbGonder" runat="server" CssClass="IK-get" ValidationGroup="ik" OnClick="lbGonder_Click" OnClientClick="ddd();" title="Gönder">Gönder</asp:LinkButton>`
<script type="text/javascript">
var ddd = (function () {
    $('body,html').animate({
        scrollTop: 300
    }, 1453);
    return false;
});

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

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