.NET Core 中 Page.Focus 的等价物是什么?
What is the equivalent of Page.Focus in .NET Core?
我一直在使用 webforms 进行编程,最近开始学习 .NET core。在 .NET Core 中是否有任何等效的 Page.Focus 或 webforms 的焦点。我在互联网上搜索并找到了一些旧的 MVC 文章,他们使用了 javascript ,想知道是否有没有 javascript.I 的解决方案有一个底部的联系我们表格,需要向下滚动,但是在提交页面再次加载后我被带到页面顶部,我需要将注意力集中在提交按钮上。
I have been programming in webforms and recently started learning .NET core .Is there any equivalent of Page.Focus or Focus of webforms in .NET Core.
目前,我们无法在 ASP.NET Core 中直接使用 WebForm 服务器控件创建 WebForm 项目。正如评论中提到的,没有可以从 ASP.NET WebForm.
迁移的等效项目模板
但是您可以尝试 Razor Pages 来简化以页面为中心的场景的编码,并且您可以在 .cshtml.cs
中定义不同的处理程序方法来处理不同的逻辑,这与您在 ASP.NET WebForm.
I have a contact us form that is at the bottom and needs to be scrolled down ,however after submit the page loads again and i am taken to the top of the page ,i need to keep the focus on submit button.
您可以尝试以下方法:
方法 1:为您的输入设置 autofocus
,这将有助于在加载页面时自动聚焦该控件。
<input type="submit" value="Submit" autofocus />
方法2:将滚动位置存储在localStorage中,然后在页面加载时根据存储的数据将window动态滚动到特定位置。
<input type="submit" value="Submit" onclick="myfunc();" />
JS代码
<script>
function myfunc() {
localStorage.setItem("scrollY", window.scrollY);
}
$(function () {
var y = localStorage.getItem("scrollY");
if (y != "" && y != null) {
window.scroll(0, y);
}
})
</script>
测试结果
我一直在使用 webforms 进行编程,最近开始学习 .NET core。在 .NET Core 中是否有任何等效的 Page.Focus 或 webforms 的焦点。我在互联网上搜索并找到了一些旧的 MVC 文章,他们使用了 javascript ,想知道是否有没有 javascript.I 的解决方案有一个底部的联系我们表格,需要向下滚动,但是在提交页面再次加载后我被带到页面顶部,我需要将注意力集中在提交按钮上。
I have been programming in webforms and recently started learning .NET core .Is there any equivalent of Page.Focus or Focus of webforms in .NET Core.
目前,我们无法在 ASP.NET Core 中直接使用 WebForm 服务器控件创建 WebForm 项目。正如评论中提到的,没有可以从 ASP.NET WebForm.
迁移的等效项目模板但是您可以尝试 Razor Pages 来简化以页面为中心的场景的编码,并且您可以在 .cshtml.cs
中定义不同的处理程序方法来处理不同的逻辑,这与您在 ASP.NET WebForm.
I have a contact us form that is at the bottom and needs to be scrolled down ,however after submit the page loads again and i am taken to the top of the page ,i need to keep the focus on submit button.
您可以尝试以下方法:
方法 1:为您的输入设置 autofocus
,这将有助于在加载页面时自动聚焦该控件。
<input type="submit" value="Submit" autofocus />
方法2:将滚动位置存储在localStorage中,然后在页面加载时根据存储的数据将window动态滚动到特定位置。
<input type="submit" value="Submit" onclick="myfunc();" />
JS代码
<script>
function myfunc() {
localStorage.setItem("scrollY", window.scrollY);
}
$(function () {
var y = localStorage.getItem("scrollY");
if (y != "" && y != null) {
window.scroll(0, y);
}
})
</script>
测试结果