C# 单击 asp.net 链接按钮将焦点设置在页面顶部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1020073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting focus on top of the page on click of asp.net link button
提问by user74042
I've a asp.net datagrid which shows customer order details. Pagination at the bottom of the grid is done using datalist and asp.net Linkbutton controls. Here is the code:
我有一个显示客户订单详细信息的 asp.net 数据网格。网格底部的分页是使用 datalist 和 asp.net Linkbutton 控件完成的。这是代码:
<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!
谢谢!
采纳答案by Amal Sirisena
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:
例如:
- Is your DataList inside an 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
- Is the Page MaintainScrollPositionOnPostBack property set to true?
- 您的 DataList 是否在 UpdatePanel 中?在这种情况下,当前滚动位置将保持在(部分)回发上。因此,您需要自己将滚动位置重置到页面顶部。一种方法是为 PageRequestManager 的 EndRequest 客户端事件实现一个处理程序,该事件将设置滚动位置 - 该线程解释了如何
- PageMaintainScrollPositionOnPostBack 属性是否设置为 true?
回答by Jacob Adams
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
回答by Ankur sharma
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. 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. ASP.NET C# Code-behind:
在 AJAX 部分回发后,您可能需要返回到 ASPX 页面的顶部以显示错误消息等。这是我完成的一种方法。您可以将下面的 JavaScript 函数添加到 ASPX 页面,然后在需要时使用 ScriptManager.RegisterClientScriptBlock 方法在代码隐藏中调用该方法。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. 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);" />
您还可以通过使用OnClientClick
按钮的属性仅使用 JavaScript 滚动到页面顶部。但这将导致每次单击按钮时都会发生这种行为,而不仅仅是在您希望它发生时发生。例如:
<asp:Button id="bntTest" runat="server"
Text="Test" OnClick="btn_Test" OnClientClick="javascript:window.scrollTo(0,0);" />
回答by Mesut ILICA
<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;
});