javascript - 自动滚动到 ASP.NET 中动态生成的控件
问题描述
所以我有一个按钮,点击时会生成一个控件。它嵌套在 div 内的更新面板中,当它在 y 方向上溢出时将创建一个滚动条。目前,当我单击按钮并且它已经溢出时,将创建控件,尽管它不会滚动到它。所以好像什么都没发生。我希望滚动条向下滚动到创建控件的位置,这样它就更加用户友好。
我试过 Page.SetFocus(control) 和 control.Focus(),但都不起作用。我查看了其他帖子,但它们并没有完全解决问题,因为我生成的控件位于 updatePanel 中。
<!-- add button and dynamic control area -->
<asp:Button ID="addStreamButton" runat="server" Text="+" OnClick="AddStreamButton_Click" Width="30px" Height="30px" Tooltip="add new stream"/>
<div class="col-sm-6">
<asp:UpdatePanel runat="server" ID="updatePanelPlaceholder" ChildrenAsTriggers="false" UpdateMode="Conditional"
style="width:650px; height:550px; overflow-y:auto; overflow-x:hidden">
<ContentTemplate>
<div class="row">
<asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="addStreamButton" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<script type="text/html">
function scrollToControl(controlName) {
document.getElementById(controlName).scrollIntoView();
}
</script>
</div>
protected void AddStreamButton_Click(object sender, EventArgs e)
{
var userControl = (DataStreamSelector)Page.LoadControl("~/DataStreamSelector.ascx");
userControl.ID = "DynamicControl" + ControlCount;
ControlCount++;
ph1.Controls.Add(userControl);
//Page.Focus(userControl);
Page.SetFocus(userControl);
}
解决方案
您需要在客户端调用它。添加此 AddStreamButton_Click 而不是焦点代码:
ScriptManager.RegisterClientScriptBlock(this, GetType(), "none", "<script>scrollToControl('" + userControl.ID.ToString() + "');</script>", false);
这将调用您已经定义的 scrollToControl 方法。
推荐阅读
- gnuplot - 使用 gnuplot 在单独的图形中绘制多个文件的每个文件
- neo4j - Neo4J - 具有重复属性或隐式存储
- jenkins - 有没有办法在单个管道中运行多个作业
- mysql - 更新包含 ID 的 mySQL 行
- sql - 如何替换旧 SQL Server 2008 版本上不支持的 TRY_CONVERT 函数?
- sql-server - 如何在 azure ms sql server 上创建可以访问所有数据库的登录名?
- laravel - Laravel 在数据透视表中创建多条记录
- outlook - C++ MAPI。管理用户和分发列表的 PR_ENTRYID
- python - 有没有办法从熊猫创建堆积条形图?
- angular - 如何传递查询参数以加载基于角度路由加载的角度组件